JavaScript String functions?

We are listing here some important and frequently used JavaScript string functions which is also asked in interview questions.

We know very well about javascript demand in these days among software and website development companies. If you want to work on strings in javascript you must be aware of string operating functions which we have brought to you. These are some of those functions which are frequently used when you work with strings in javascript. 

1- charAt(): This function returns the character at specified index in a string.

var name = "Hello Guys!";

var res = name.charAt(2);

console.log( res ); 

 

2- concat(): This method is used to join two or more strings.

string.concat(string1, string2, ..., stringX)

var str1 = "Hello ";

var str2 = "world!";

var res = str1.concat(str2);

console.log( res ); 

 

3- includes():  This methods checks whether the string contains the substring characters. If contains will return true else return false. It is case sensitive.

var str = "The world is full of joy and love";

var res = str.includes("world");

console.log( res ); 

 

4- indexOf():  This method returns index of first occurrence of the string and returns -1 if does not find it in string.

var str = "The world is full of joy and love";

var res = str.indexOf("world");

console.log( res ); 

 

5- length: The length property returns the length of the string.

var str = "The world is full of joy and love";

var res = str.length;

console.log( res ); 

 

6- match():  This method search for a string to match against a regular expression and returns the matches in an array of objects. If no match found it returns null. If you don't use g global it will return only first match.

var str = "The rain stays mainly in the plain";

var res = str.match(/ain/g);

console.log( res ); 

 

7- replace(): This method replaces a specified string into another string and returns the new one. To replace all occurrences of the string use regular expression else it will replace the first occurrence only. 

var str = "Mr Blue has a blue shirt and a blue car";

var res = str.replace(/blue/g, "red");

console.log( res ); 

 

8- slice():  This method returns the extracted part of string, for this you need to pass start and end position.

var str = "The world is full of joy and love";
var res = str.slice(0, 5);

console.log( res ); 

 

9- split():  This method splits a string into an array of substrings.

var str = "The world is full of joy and love";

var res = str.split(" ");

console.log( res ); 

 

10- substr():  This method return a substring from a string specified by start position and number of characters.

var str = "Mr Blue has a blue shirt and a blue car";

var res = str.substr(1, 5); 

console.log( res ); 

 

11- trim(): This method trims the white spaces from both side of string.

var str = "       Hello World!        ";

console.log(str.trim());

 

12- toLowerCase():  This method returns lowercase letters of the string.

var str = "Mr Blue has a blue shirt and a blue car";

var res = str.toLowerCase();

console.log(res);