Some Important JavaScript String Methods

The JavaScript string slice() method is used to fetch the part of the string and returns the new string. It required to specify the index number as the start and end parameters to fetch the part of the string. The index starts from 0. This method allows us to pass a negative number as an index. In such a case, the method starts fetching from the end of the string. It doesn’t make any change in the original string.

2. subString() :

3. substr() :

4. replace() :

We can use a global search modifier with replace() method to replace all the match elements otherwise the method replace only the first match. JavaScript also provides ignore flags to make the method case-insensitive.

The replace() method is represented by the following syntax:


It is case sensitive but if we use /i for case insensitive

str = “Please visit yahoo!”;
var n = str.replace(/YAHOO/i, “Google”);

To replace all matches, use a regular expression with a /g flag (global match):

str = “Please visit yahoo and yahoo!”;
var n = str.replace(/yahoo/g, “Google”);

5. indexOf() :

The index position of the first character in a string always starts with zero. If an element is not present in a string, it returns -1.

It gives two-argument first is searchValue and the second is fromIndex

Syntex: str.indexOf(searchValue[, fromIndex])

An empty string searchValue produces strange results. With no fromIndex value, or any fromIndex a value lower than the string's length, the returned value is the same as the fromIndex value:

'hello world'.indexOf('') // returns 0
'hello world'.indexOf('', 0) // returns 0
'hello world'.indexOf('', 3) // returns 3
'hello world'.indexOf('', 8) // returns 8

6. charAt() :

The index number starts from 0 and goes to n-1, where n is the length of the string. The index value can’t be a negative, greater than or equal to the length of the string.

The charAt() method is represented by the following syntax:


index — It represents the position of a character.

return A char value


const str = ‘Hello world’;

const result = str.charAt(1);

console.log(resule); // “e”

9. concat() :

The concat() method is represented by the following

syntax: string.concat(str1,str2,…,strn)


str1,str2,…,strn — It represent the strings to be combined.


Combination of strings.

8. toLowerCase :

The toLowerCase() method is represented by the following syntax:


9. toUpperCase:

The toUpperCase() method is represented by the following syntax:


10. Split():


var txt = “a,b,c,d,e”; // String
txt.split(“,”); // Split on commas
txt.split(“ “); // Split on spaces
txt.split(“|”); // Split on pipe

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

If the separator is “ ”, the returned array will be an array of single characters:

var txt = “Hello”; // String
txt.split(“ ”); // Split in characters

I am Naiemul Islam. I am a professional web Developer