Strings in JavaScript

Elijah Samuels
3 min readAug 27, 2021

So I wanted to take a short pause from the arrays, and go back to the string objects.

The first method I wanted to cover is the .slice() mainly because it’s also an array method, and I felt like it’s a great segue.

.slice()

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

Slice(startingIndex, endingIndex)

Think of this just like above: start slicing up the string here, end the slicing of the string here. You can also work backwards utilizing a negative number. Yes yes yes… the examples.

So for testString1.slice(5), we’re starting at the sixth index from the beginning. Remember, we start with the zeroth place.

If we use a negative number, we start from the end of the string and work right-to-left. With testString1.slice(-4), we’re returning everything after the negative fourth index.

We can also start from the beginning AND work backwards from the end, to create a delimiter.

For fun, I wanted to extract “RING of T” utilizing both ways: with the negative numbers and positive numbers working from the beginning. The next step is to prove that each way is exactly equal to each other.

.split()

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

So we’re going to get an array object from the string.

In the above example, we’re not split the string by anything, so it returns in within an array. I also went ahead to make a new variable as an array object from the string itself. Now here’s the weird thing, when comparing the two, they’re not the same… kind of. They do have the same return value, same length, and properties, so what else is different? Well, I believe it’s the fact that while newTestString4 and newTestString4a are both objects, they both DIFFERENT objects, and as a result, have different places in memory.

Next, some simple but very handy methods.

.trimStart()

The trimStart() method removes whitespace from the beginning of a string. trimLeft() is an alias of this method.

.trimEnd()

The trimEnd() method removes whitespace from the end of a string. trimRight() is an alias of this method.

.trim()

The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

Pretty straight-forward, and shouldn’t need much explaination. We’re removing unnecessary white space from a string. Great trick to use in a form so that a users last name doesn’t look like “ Smith ” but instead you’d have “Smith”.

The last couple of really handy and easy to use string methods are .toUpperCase() and .toLowerCase()

Just as you can guess, these make your string UPPER or lower cased.

And kind reader, if you’re asking, “When would I ever use one of these?! I mean, toLowerCase doesn’t seem that handy.” Hold my mouse…

In all seriousness though, there are times when you want to utilize a lowercased string to compare it to something within your database. As we all know:

--

--