1.trim() :
This method removes whitespace from both sides of a string.

let text1 = " hey hi! "; let text2 = text1.trim(); //hey hi! 

2.trimStart() :
This method removes whitespace only from start of a string.

let text1 = " hey hi! "; let text2 = text1.trim(); //hey hi! 

3.trimEnd() :
This method removes whitespace only from end of a string.

let text1 = " hey hi! "; let text2 = text1.trim(); // hey hi! 

4.repeat() :
method returns a string along with a number of copies of a string.

let text = "python"; let result = text.repeat(2); //pythonpython 

5.replace() :
method replaces a specified value with another value in a string.
note :

  • method does not change the string.
  • method replaces only the first match.
let text = "Please visit Microsoft!"; let newText = text.replace("Microsoft", "W3Schools"); //Please visit W3Schools! 

the replace() method replaces only the first match.

let text = "Please visit Microsoft and Microsoft!"; let newText = text.replace("Microsoft", "W3Schools"); //Please visit W3Schools and Microsoft! 

the replace() method is case sensitive. Writing MICROSOFT (with upper-case) will not work.

let text = "Please visit Microsoft!"; let newText = text.replace("MICROSOFT", "W3Schools"); //Please visit Microsoft! 

To replace case insensitive, use a regular expression with an /i flag.

let text = "Please visit Microsoft!"; let newText = text.replace(/MICROSOFT/i, "W3Schools"); //output : Please visit W3Schools! 

To replace all matches, use a regular expression with a /g flag.

let text = "Please visit Microsoft and Microsoft!"; let newText = text.replace(/Microsoft/g, "W3Schools"); output : Please visit W3Schools and W3Schools! 

6.replaceAll() :
note : replaceAll() does not work in Internet Explorer.

let text = "I love cats. Cats are very easy to love. Cats are very popular." text = text.replaceAll("Cats","Dogs"); text = text.replaceAll("cats","dogs"); //output : I love dogs. Dogs are very easy to love. Dogs are very polpular. 

regular expression, the global flag (g) must be set.

text = text.replaceAll(/Cats/g,"Dogs"); text = text.replaceAll(/cats/g,"dogs"); 

7.split() :
A string can be converted to an array with the split() method.

let text = "Hi fox!"; const myArr = text.split(""); //["H","i","f","o","x","!"] 
syntax : text.split("") // Split on characters text.split(",") // Split on commas text.split(" ") // Split on spaces text.split("|") // Split on pipe 

If the separator is omitted, the returned array will contain the whole string in index [0].

let text = "The quick brown fox."; const myArr = text.split(); //["The quick brown fox."]