25 JavaScript String Methods Cheat Sheet for Developers

JavaScript has strong built-in string methods that allow developers to easily work with and format text. No matter if you are new to coding or have years of experience, learning these string methods can simplify your programming tasks. Here’s a quick reference of 25 key JavaScript string methods with examples!

JavaScript String Methods

1. length

It returns the number of characters in a string.


  let str1 = "Hello World";
console.log(str1.length); // Output: 11

2. charAt()

Provides the character found at a specific index.


  
const str = "Uvais Codes";
console.log (str.charAt(0));  // Output: U

3. charCodeAt()

Shows the Unicode for the character at a specific index.


    
const str = "Uvais Codes";
console.log(str.charCodeAt(3)); //Output: 105

4. toUpperCase()

Creates a new string where all lowercase letters are changed to uppercase.


  
const str1 = "Hello World";
console.log(str1.toUpperCase()); //Output: HELLO WORLD

5. toLowerCase()

Creates a new string where all uppercase letters are changed to lowercase.


    
const str1 = "Hello World";
console.log(str1.toLowerCase()); // // Output: hello world

6. slice()

Creates a new string that includes the characters from a specific index to the end of the string.


    
const str = "Hello World";
console.log(str.slice(4)); //Output: o World

7. substring()

The substring() method extracts a certain part of the string using the start and end indexes. It is similar to slice, but does not accept negative indexes.


  
const str = "Javascript";
console.log(str.substring(0, 5)); // Output: Javas


8. trim()

The trim() method removes whitespace from both sides of a string.


    
let text = "   JavaScript   ";
console.log(text.trim()); // Output: "JavaScript"

9. trimStart()

The trimStart() method removes whitespace only from the beginning.


    
let text = "   JavaScript   ";
console.log(text.trimStart()); //Output: "JavaScript  "

10. trimEnd()

The trimStart() method removes whitespace only from the end.

    
let text = "   JavaScript   ";
console.log(text.trimEnd());  //Output: "  JavaScript"

11. String.concat()

The concat() method is used to combine two or more strings


    
let str1 = "Uvais";
let str2 = "Codes";
console.log(str1 + str2); // Output: "UvaisCodes"
console.log(str1.concat(" ", str2)); // Output: "Uvais Codes"

12. endsWith()

It Checks whether a string ends with certain characters.


    
let str1 = "Uvais Codes";
console.log(str1.endsWith("World"));  //Output: false

13. includes()

It Verifies if a string has the given string or characters.


    
let str1 = "Uvais Codes";
console.log(str1.includes("Uvais")); //Output : 'true'

14. indexOf()

Shows where the specified value first appears in a string.


    
let str1 = "Uvais Codes";
console.log(str1.indexOf("a")); //Output :'2'

15. lastIndexOf()

It finds where the specified value last shows up in a string.


    
let str1 = "Uvais Codes";
console.log(str1.lastIndexOf("s")); //Output : '10'

16. split()

The split() method is used to split a strin into an array


    
const str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]

17. replace()

The replace() method in JavaScript is used to change a specific part of a string to a new string. It only replaces the first occurrence of the given value.


    
const str = "Hello World";
console.log(str.replace("World", "JavaScript")); // Output: Hello JavaScript

18. replaceAll()

The replaceAll() method is used to change all occurrences of a certain value in a string. Unlike the replace() method, which only change the first match, replaceAll() replaces every instance of the specified substring.


    
let newStr = "Hello World World";
console.log(newStr.replaceAll("World", "JS")); // Output: Hello JS JS

19. startWith()

The startsWith() method checks if a string starts with a specific substring. It returns true if the string starts with that substring and false if it does not.


    
let str = "Hello World World";
console.log(str.startsWith("Hello")); // Output: true

20. padStart()

In JavaScript, the padStart() method helps to fill the start of a string with another string until it meets the specified length.


    
let num = "8";
console.log(num.padStart(3, "0")); 
// Output: "008"

21. padEnd()

The padEnd() method in JavaScript adds padding to the end of a string with another string until it reaches the desired length.


    
let str = "5";
console.log(str.padEnd(3, "0")); 
// Output: "500"

22. repeat()

The repeat(count) function in JavaScript creates a new string by repeating the original string as many times as you specify. This is great for when you need to repeat a string several times.


    
let str = "Hello ";
console.log(str.repeat(3)); // Output: "Hello Hello Hello "

23. search()

The search() method in JavaScript helps you find where the first match of a specific regular expression appears in a string. If it doesn't find a match, it gives back -1.


    
let str = "JavaScript is awesome!";
console.log(str.search(/is/)); // Output: 11 (Index where "is" starts)

24. substr()

the substr() gets a portion of a string by using a starting point and a defined length of characters.

Keep in mind that substr() is outdated. It still functions in current browsers, but it's better to use slice() or substring().


    
let str = "Hello World";
console.log(str.substr(0, 5)); // Output: "Hello"

25. toString()

The toString() method in JavaScript changes a value into a string. If it's used on a string, it simply gives back that same string.


    
let num = 123;
console.log(num.toString()); // Output: "123"

Conclusion

Learning these 25 JavaScript string methods will simplify text handling and improve your coding speed. Keep this cheat sheet handy for quick help when dealing with JavaScript strings.

Must Read If you haven't

Thanks for reading

If you have any questions or need more information, feel free to leave a comment.


Previous Post Next Post