Some Javascript String methods that you should know

Nahid Hossain Nehal
4 min readNov 2, 2020
  1. charAt(): charAt() method will return you the character of your given index number from the string that you have been provided.

If you don’t provide any index number as a parameter, it’ll take 0 as a default parameter.

2. indexOf(): The indexOf() method is just an alternative to the charAt() method. In this indexOf() method your parameter will be a character and the method will return you the index number of the character

If the value not found then the method will return -1

3. lastIndexOf(): If any character has been used several times in a sentence or word then the lastIndexOf() method will return you the last index number of this character, for instance

It will also return -1 if the value is not found.

4. include(): By include() methods we can check a specific word that contains in a sentence or not. This method returns a boolean value.

Remember one thing include() method is case sensitive, for instance:

5. replace(): The replace() method is used to replace any word with a pattern from a sentence. And the pattern can be RegExp or a string. If the pattern is a string then the first match will be changed but if the pattern is RegExp then all matches will be changed.

But the main string will remain unchanged.

6. slice(): The slice() method returns a new string extract text from the main string. The text will be extracted by providing index numbers. if the index number is negative then first of all the given negative index number will be minus from the length of the string then its result will be used as a parameter if the providing parameter is greater than string length than it will return an empty string

7. startsWith(): By startsWith() method, you can check a string whether it starts with the characters of a particular string. This is case sensitive

8. toLowerCase(): The toLowerCase() method return string by converting the given string as an all lowercase.

9. toUpperCase(): The toUpperCase() method the string by converting the given string all uppercase.

10. trim(): The trim() method return string by removing the white space from the beginning and end of the string

11. trimStart(): The trimStart() method only removes white space from the beginning of the string.

12. trimEnd(): The trimEnd() method only removes white space from the End of the string.

--

--