Some Important JavaScript String Methods

1. slice() :

Naiem
3 min readNov 2, 2020

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() :

subString() similar to the slice() method but the difference is it can not accept a negative index

3. substr() :

This is also similar to slice(). substr() extracts length characters from a str, counting from the start index.

4. replace() :

The JavaScript string replace() method is used to replace a part of a given string with a new substring. This method searches for a specified regular expression in a given string and then replace it if the match occurs.

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:

string.replace(originalstr,newstr);

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 JavaScript string indexOf() method is used to search the position of a particular character or string in a sequence of given char values. This method is case-sensitive.

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() :

charAt() is a javascript string method, this is the character access method.The JavaScript string charAt() method is used to find out a char value present at the specified index in a string.

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:

string.charAt(index)

index — It represents the position of a character.

return A char value

example:

const str = ‘Hello world’;

const result = str.charAt(1);

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

9. concat() :

The JavaScript string concat() method combines two or more strings and returns a new string. This method doesn’t make any change in the original string.

The concat() method is represented by the following

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

parameter

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

Return

Combination of strings.

8. toLowerCase :

The JavaScript string toLowerCase() method is used to convert the string into the lowercase letter. This method doesn’t make any change in the original string.

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

string.toLowerCase()

9. toUpperCase:

The JavaScript string toUpperCase() method is used to convert the string into a uppercase letter. This method doesn’t make any change in the original string.

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

string.toUpperCase()

10. Split():

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

Example:

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

--

--

Naiem

I am Naiemul Islam. I am a professional web Developer