JavaScript String Methods

Each coding language has its own syntax for string methods. A string is a sequence of characters. These characters are often letters, but can include symbolic characters as well. A method is a set of instructions that tell the computer what to do in a certain order, and are built-in to your computer. String methods manipulate data that is entered as a string. In this article, I will walk you through the most common string methods. These come in handy when learning to code or looking for your first programming job. You can find live examples of each of these on my page. Click here.

Length

Measuring GIFs | Tenor

The easiest string method to know is the length method. It tells you how long a string is. The string "hello" has 5 characters, so "5" will be returned. In JavaScript, the syntax for this method is

variableName.length

where variableName is the string you would like to know the length of.

Split

The "split" method is used when you would like to access only a few letters of a string. Many interview problems could potentially require writing a line with a split in it. The split method is used for converting a string into an array of separate strings. The parameter passed into this method is the character that the string will be separated by; this can be a space, a comma, a quote, and more.

Slice

Slice Cake GIF - Tenor GIF Keyboard - Bring Personality To Your  Conversations | Say more with Tenor | Anime cake, Food, Food and drink

The slice method allows you to return part of a string. When an index is entered, the string will be returned with all values past that index. Let's say we have a string called colorString with "red, orange, yellow". If we enter

colorString[5]

what we get as a return is "orange, yellow". The letter "r" in the original string has an index of 0. The index of 4 is the space character before the word "orange." The slice method removes everything from before the 5th index and returns what is left. It may help to imagine that all characters from index 0 to the index selected are removed.

Try entering a negative number and see what is returned.

Includes

The includes method returns a boolean value of True or False if a string contains the parameter that is passed in. This will verify if the second value is a substring of the first.

CharAt

Searching GIFs - Get the best GIF on GIPHY

The charAt method returns a character at the given index of a string. Let's say we have a string called "someString".

someString.charAt(5)

will return "t".

This becomes useful when working with long strings.

IndexOf

IndexOf finds the index, or numerical position, of a character in a string. Strings indices begin at 0. So, in the word "fox", the character "o" has an index of 1. IndexOf will return the index of a specified character. If that character does not occur in a string, -1 will be returned. Because of this, IndexOf can be used interchangeably with the "includes" method depending on the circumstance.

Additional Notes

There are many other string methods to learn. Understand your learning style and dig deeper into string methods. They are used in coding interviews and on the job.

8 Must Know JavaScript Array Methods is a YouTube video that helps explain string methods further.

MDM Web Doc's Useful String Methods has practical coding examples of these methods.

Happy coding!