The String is one the most used objects in JavaScript, which is why it is so important to know its properties and methods. To clear up some confusion, you already know this object as a string variable. As I just explained in the previous tutorial, this variable was created by a string() function. So, this variable is actually an object with properties and methods.
The string is a pretty large class because developers want to do some many different things with strings. The idea of a string in itself is very complicated. You might want to know how many “e” characters are in it, but you might also want to know the total number of characters in the string. Those are just two common reasons why we have properties. JavaScript does a really good job of providing us with a wealth of properties to get what we need from our strings.
Common String Properties
constructor – tells us the function that created the String object’s prototype
length – returns the total number of characters in a string
Common String Methods
A string is a very changeable variable, which means you need to be careful when using string methods. Many of the string methods will manipulate the initial value of the string, which cannot be recovered. You might have noticed already, but methods are easily distinguished by the fact they have () following them.
charAt() – returns character using its index
indexOf() – returns the index of the first instance of a specified value
lastIndexOf() – returns the index of the last instance of a specified value
match() – finds all matches in the string of a specified value
replace() – finds all matches in the string of a specified value and then replaces them with a new substring
search() – looks for a match in the string with a specified substring and returns the substrings index in the string
slice() – extracts and returns part of the string
split() – splits string into an array of substrings
substr() – extracts characters from a string starting at a specified position and ending at a specified length from the starting position
substring() – extracts characters from a string starting between two specified positions
toLowerCase() – converts all letters in string into lowercase
toUpperCase() – converts all letters in string into uppercase