Some important String methods of JavaScript every Web Developer needs to know

MD. AMINUL ISLAM
3 min readNov 2, 2020
JavaScript String Methods
  1. charAt()
    Suppose, you want to return the character at a specified index in a string then you may use the charAt() method. Where 0 is the index of the first character, 1 is the index of the second character, and so on.

Syntax:

string.charAt(index)

Example:

var sampleString = “New String”;
var requiredCharacter = sampleString.charAt(0);
console.log(requiredCharacter);

If you to return the last character of the string then

var sampleString = “New String”;
var lastCharacter = sampleString.charAt(sampleString.length-1);
console.log(requiredCharacter);

2. concat()
Sometimes you do not need to change the existing strings, but return a new string combining two or more strings then you may use the concat() method.

Syntax:

string.concat(string1, string2, …, stringN)

Example:

var sampleString1 = “String1”;
var sampleString2 = “String2”;
var concatedString = sampleString1.concat(sampleString2);
console.log(concatedString);

3. includes()
When you need to determine whether a string contains the characters of a specified string or not then you may use the includes() method.

Syntax:

string.includes(searchString, start)

Example:

const sampleSentence = “Hello all from JS”;
const sampleWord = “JS”;
console.log(`”${sampleWord}” ${sampleSentence.includes(sampleWord) ? ‘is’ : ‘is not’} found in the sentence`);

4. endsWith()
When you need to determine whether a string ends with the characters of a specified string or not then you may use the endsWith() method.

Syntax:

string.endsWith(searchString, length)

Example:

const sampleString = “Hello all from JS”;
console.log(sampleString.endsWith(‘JS’, sampleString1.length));

5. indexOf()
Whether you want to return the position of the first occurrence of a specified value in a string you may use the indexOf() method. If the value to search for never occurs or not found this method returns -1.

Syntax:

string.indexOf(searchString, start)

Example:

const sampleString = “Hello all from JS. JavaScript, often abbreviated as JS”;
console.log(sampleString.indexOf(‘JS’));

6. lastIndexOf()
Whether you want to return the position of the last occurrence of a specified value in a string you may use the lastIndexOf() method. If the value to search for never occurs or not found this method returns -1.

Syntax:

string.lastIndexOf(searchString, start)

Example:

const sampleString = “Hello all from JS. JavaScript, often abbreviated as JS”;
console.log(sampleString.lastIndexOf(‘JS’));

7. replace()
The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. If pattern is a string, only the first occurrence will be replaced.

Syntax:

string.replace(searchString, newString)

Example:

const sampleString = “Hello all from JS. JavaScript, often abbreviated as JS”;
console.log(sampleString.replace(“JS”, “World”));

8. slice()
Sometimes you need to extracts parts of a string without modifying the original string and slice() method plays the role here. It returns the extracted parts in a new string.
You need to use the start and end parameters to specify the part of the string you want to extract. The first character has the position 0, the second has position 1, and so on.
If you want to select from the end of the string you have to use a negative number.

Syntax:

string.slice(startIndex, endIndex)

Example:

const sampleString = “JavaScript, often abbreviated as JS”;
console.log(sampleString.slice(0, 9));
console.log(sampleString.slice(-5, -3));
console.log(sampleString.slice(-2));

9. split()
When you need to split a string into an array of substrings you may use the split() method.
The split(“ “) method split between each word.
The split(“”) method split between each character.
The split() method does not change the original string.

Syntax:

string.split(separator, limit)

Example:

const sampleString = “JavaScript often abbreviated as JS”;
console.log(sampleString.split(“ “));
console.log(sampleString.split(“”));
console.log(sampleString.split());

10. substr()
Besides the slice() method to extract parts of a string you may use the substr() method. The only difference is that it begins with the index specified position and ends with the number of characters to extract

Syntax:

string.substr(startIndex, length)

Example:

const sampleString = “JavaScript, often abbreviated as JS”;
console.log(sampleString.substr(0, 9));
console.log(sampleString.substr(0));

--

--

MD. AMINUL ISLAM

B.Sc. in Computer Science and Engineering. University of Barishal, Barishal.