String

Abdul Jabbar Babu
2 min readMay 5, 2021

String is one the most important data-type of Javascript. Today we will try to explore some of the important methods of String.

charAt() method :

In this method, one can easily find out the character at a specified index in a string. In Javascript position is described as index. The first index of any string is ‘0’ and the last index is ‘string.length — 1’.

const str = ‘Bangladesh is my native country’;

console.log(str.charAt(7));

// the result will be — e

console.log(str.charAt());

// the result will be — 0

If you provide index is out of range, It will return empty string and not provided index charAt() the default value is 0.

concat() method:

It concatenate or link two or more things in a chain.

const str1 = ‘Hello’;

const str2 = ‘Bangladesh’;

console.log(str1.concat(‘ ‘, str2));

console.log(str1.concat(‘ ‘, ‘Bangladesh’)); // both the result will be Hello Bangladesh

indexOf() method:

In this method we can easily find out the position of any character or starting position of a word in string. Index in string is count from left to right.

const str = ‘Bangladesh is my native country’;

console.log(str.indexOf(‘my’));

// the result will be — 14

If the value is not found it returns -1.

lastIndexOf() method:

if there are same word or character contains two or more times in a string in this method we can find the last position of that word or character.

const str = ‘Bangladesh is my native country. Bangla is my favorite language’;

console.log(str.lastIndexOf(‘my’));

// the result will be — 43

Replace() method:

In this method, we can change any value from a string.

const str = ‘I like Banana’;

console.log(str.replace(‘Banana’, ‘Mango’));

// the result will be — ‘I like Mango’

Slice() method:

This method extracts value from a string and returns a new string. This method has to provide start and end parameters for specified the extracting position in string.

const str = ‘Hello Bangladesh’;

console.log(str.slice(0, 5));

// the result will be — Hello

Split() method:

In this method, a string is divided into substring and returns an array. In this method have to mention the separator.

const str = ‘I live in Bangladesh’;

const split = str.split( ‘ ‘);

console.log(split);

// the result will be [ ‘I’, ‘live’, ‘in’, ‘Bangladesh’ ]

If provide empty string (split(‘’)) as a separator then every single character will convert into a string.

Includes() method:

In this method determines whether a string contains specified character or not. If the character contains It returns true, if not false.

const str = ‘I live in Bangladesh’;

console.log(str.includes(‘live’));

// the result will be — true

console.log(str.includes(‘lives’));

// the result will be — false

toLowerCase() method:

This method converted the value of a string into lower case.

const str = ‘I Live In Bangladesh’;

console.log(str.toLowerCase());

// the result will be ‘i live in bangladesh’

toUpperCase() method:

This method converted the value of a string into upper case.

const str = ‘I Live In Bangladesh’;

console.log(str.toUpperCase());

// the result will be ‘I LIVE IN BANGLADESH’

--

--