Sitemap

14 Most useful JavaScript String Methods

3 min readNov 2, 2020
Press enter or click to view image in full size

Strings are simply groups of characters, like ‘Hello’, ‘Good Morning!’. They are an important global object in JavaScript. They represent a sequence of characters. JavaScript has many powerful built-in functions that make working with strings easy for developers. Here I will explain some most important methods s of javascript strings.

  1. string.indexOf(substr, [start]) — returns the index position of character value passed with method. If not found, -1 is returned.

2. string.lastIndexOf(substr, [end]) — returns the last index position of character value passed with method. If not found, -1 is returned.

3. string.replace(originalstr,newstr) — search for a specified regular expression in a given string and then replace it if the match occurs.

4. string.slice(start, [end]) — returns a substring of the string based on the “start” and “end” index arguments.

5. string. split() — converts a string into an array. You have to pass a character such as a comma (,) or space to tell where to split the string.

Press enter or click to view image in full size

6. string.trim() — returns a new string with removed white space from the start and end of the string.

7. string.trimStart() — returns a new string with removed white space from the start of the string.

8. string.trimEtart() — returns a new string with removed white space from the end of the string.

9. string.charAt(x) — Returns the character at the “x” position within the string.

10. string.concat(string1, string2, …) — Combines one or more strings into the existing one and returns the combined string.

Note: Original string is not modified.

11. string.includes(searchString) — returns whether searching String may be found in the source string.

12. string.toUpperCase() — Returns the string with all of its characters converted to uppercase.

This method doesn’t make any change in the original string.

13. string.toLowerCase() — Returns the string with all of its characters converted to lowercase.

This method doesn’t make any change in the original string.

14. string.ends With(searchString[, length]) — returns whether the source string ends with the searching string.

I hope this lesson provides a clear understands of how to string methods works in JavaScript. Hopefully, you learned something today. Happy Coding.

--

--

No responses yet