Several String methods should be known in JavaScript

Tram Ho

String is one of the most important parts in javascript, apart from the commonly used methods such as trim , concat , subString , toUpperCase , toLowerCase ; Javascript also provides us with many other useful methods to make manipulating and solving problems easier when working with String. Although these functions have been around for a long time or you have also used them, this is an opportunity for us to review and better understand the use of each function, let’s get started !!!

1. includes

includes method is used to test whether a string contains another string or not.

The following example checks whether the word ‘world’ is in the string or not:

2. repeat

repeat method will create a new string that repeats the specified number of times of the original string.

The following example creates a new string with 5 iterations of the original string:

3. match

match method will search for a string match with the passed regular expression, and return an array of strings that were just searched; if not found, null is returned.

The following example:

4. charAt

charAt method will return a character in the string at the specified index position

The following example:

5. indexOf

indexOf method will return the first index position of a string in the original string, otherwise it returns -1

The following example:

6. replace

replace method will return a new string with the replaced values ​​of the original string.

The following example:

7. padStart, padEnd

padStart adds a number of characters in front of the original string with a specified length.

padEnd adds a number of characters after the original string with a specified length.

The following example:

8. Convert string to array

There are many ways to convert strings to array of characters, but I prefer to use the spread operator.

The following example:

9. Convert number to string

10. Convert string to number


Above are the string methods that we need to know to apply well in all situations, I hope to help beginners understand and use string effectively in javascript. Thank you for reading the article.

Share the news now

Source : Viblo