To get rid of those nightmares, we need to know the most essential properties and methods of strings in JavaScript. Let’s explore them one by one.

length

The property length returns the length of the string.

toUpperCase()

The method toUpperCase turns every character in the string to the upper case and returns it. It doesn’t change the original string.

toLowerCase()

The method toLowerCase turns every character in the string to the lower case and returns it. It doesn’t change the original string.

trim()

The method trim removes the starting and ending white spaces from the string. It is an in-place operation i.e.., updates the original string.

charAt(index)

The method charAt returns the character at the given index. It returns an empty string if the index is not valid.

charCodeAt(index)

The method charCodeAt returns the character ASCII code at the given index. It returns NaN if the index is not valid.

slice(startIndex, endIndex)

The method slice returns the substring from the string from startIndex to endIndex (not including). The string.slice(0, 6) returns the substring from the 0th index to 5th index. The method slice will accept a sing argument as well. If you pass a single argument to the slice method, then it will return the substring from the given index to the end of the string. The method slice will accept negative indexes as well. The negative indexes are counted from the end of the string. Let’s see an example as it is new to most people. Given string GeekFlare, the negative indexes are G = -9, e = -8, e = -7, k = -6 and so on… The code string.slice(-9, -5) will return Geek for the above example. The code string.slice(-5) will return Flare for the above example. Note: Negative indexing will not work in IE8 and earlier versions.

substr(startIndex, length)

The method substr is similar to the slice method. The only difference is that the method substr accepts the substring length that needs to be extracted from the original string. There is another method called substring which is similar to the slice method. But, the method substring won’t accept negative indexes. Try it out.

replace(substring, newSubstring)

The method replace replaces the first instance of the substring with the newSubString.

indexOf(substring)

The method indexOf returns the starting index of a given character from the string. It will return -1 if the character doesn’t present in the string. The method indexOf will accept the second argument which is an index from which the searching starts for the given substring. There is another method called lastIndexOf which is similar to the method indexOf. The only difference is that the method lastIndexOf searches the character from the ending of the string and returns the index of the first instance of the character. Try it out for the code company.lastIndexOf(’e’).

split(substring)

The method split splits the given string on the substring and returns the parts as an array.

Conclusion

This is not the end. Explore the remaining methods of the strings from the documentation. There might be other methods that are useful in specific cases. Search and use them in your specific if it’s not listed here. Happy Coding 🙂 Next, explore some of the popular JavaScript frameworks.

String Methods to Know in JavaScript - 76String Methods to Know in JavaScript - 49String Methods to Know in JavaScript - 34String Methods to Know in JavaScript - 35String Methods to Know in JavaScript - 72String Methods to Know in JavaScript - 46String Methods to Know in JavaScript - 16String Methods to Know in JavaScript - 98String Methods to Know in JavaScript - 95String Methods to Know in JavaScript - 91String Methods to Know in JavaScript - 49String Methods to Know in JavaScript - 78String Methods to Know in JavaScript - 60String Methods to Know in JavaScript - 19String Methods to Know in JavaScript - 66String Methods to Know in JavaScript - 62String Methods to Know in JavaScript - 63String Methods to Know in JavaScript - 73String Methods to Know in JavaScript - 14String Methods to Know in JavaScript - 71String Methods to Know in JavaScript - 91String Methods to Know in JavaScript - 89String Methods to Know in JavaScript - 30