13 Most Common JavaScript String Methods You Should Know About

In JavaScript, strings are sequences of characters. JavaScript provides a rich set of methods to manipulate and work with strings. In this post, I’ll introduce you to the 13 most commonly used JavaScript string methods and their functionalities.

String length

If you want to find the number of characters in a string, then you can use the length property.

This also counts white spaces.

String toUpperCase()

If you want to convert a string to uppercase, then you can use the toUpperCase() method.

String toLowerCase()

If you want to convert a string to lowercase, then you can use the toLowerCase() method.

String indexOf()

If you want to find the first occurrence of a substring in a string then you can use the indexOf() method.

String lastIndexOf()

If you want to find the last occurrence of a substring in a string then you can use the lastIndexOf() method.

String slice()

If you want to extract a part of a string, then you can use the slice() method. This will return the extracted part in a new string.

Syntax:

The end position will not be included.

If you don’t specify the end position then this will slice out the rest of the string.
For example:

You can also give it negative parameters.

For example:

To put it simply, you can understand this as:

String substring()

The substring() method is similar to the slice() method, but the difference is that if you give it negative parameters (less than 0) then they will be treated as 0.

String substr()

The substr() method is similar to the slice() method, but the difference is that the end parameter is the length of the characters to be extracted.

String charAt()

If you want to get a character at a specified index in a string, then you can use the charAt() method.

String concat()

If you want to concatenate two or more strings, then you can use the concat() method.

String trim()

You can remove whitespace from both ends of a string using the trim() method.

String replace()

If you want to replace a specified substring with another string, then you can use the replace() method.

String split()

You can convert a string into an array using the split() method.

That’s all for today.

Thanks for reading.

For more content like this click here.

You can also follow me on X(Twitter) for getting daily tips on web development.

Keep Coding!!

Leave a Comment