10 Most important methods about Javascript string

Asif Ur Rahman
2 min readMay 5, 2021

--

Javascript, the language which changes the user interface of the website overnight. It works just incredibly. day by day the use of javascript is increasing. At first, it was used in the front end only. But now it is a famous language in the backend also.

A string is a collection of characters. Where the first index starts from 0.

  1. CharAt():

A method that is used in string operation. It is used to access an individual character in the string. Such as

const name = “asif ur rahman”;console.log(name.charAt(1)); //returns s.

2. Concat():

This method is used to contact several strings. Such as

const firstName = “asif ur”const lastName = “rahman”const fullName = firstName.concat(lastName)console.log(fullName)// returns asif ur rahman.

3. includes()

This is a case-sensitive method. It works with true and false. a specific string is passed through this includes method. If the string is matched with a given string then it will return true. If no match occurs then it will pass false.

const str = “I am Md. Asif Ur Rahman”;const result1 = str.includes(“Asif”);const result2 = str.includes(“World”);console.log(result1) // return trueconsole.log(result2) // return false

4. endsWith()

It works like boolean algebra which means true or false. It checks if a given string ends with the string which is passed through endsWith().

const str = “I am Md. Asif Ur Rahman”;console.log(str.endsWith(‘Rahman’)); //trueconsole.log(str.endsWith(‘Ur’)); //false

5.indexOf()

It is a case-sensitive method that identifies the first occurrence of a string.

var str = “I am Md. Asif Ur Rahman”;console.log(str.indexOf(“Ur”)) // return 14

6. lastIndexOf()

It identifies the last occurrence of a specified string.

var str = “I am Md. Asif Ur Rahman. I am a programmer.”;console.log(str.lastIndexOf(“am”)); // 36

7.replace()

It identifies a specific string and replaces it with another string.

const str = “I am Md. Asif Ur Rahman. I am a programmer.”;console.log(str.replace(“programmer”, “codder”)); //I am Md. Asif Ur Rahman. I am a codder.

8. slice(a,b)

Slice means to extract some character in a given string. Where a and b identify the index. a is the first index and b is the last index. The slice method will cut between these two index.

const name = “Asif Ur Rahman”console.log(name.slice(8,14)) // Rahman

9. split()

This split method split the total string as a collection of arrays. It does not change the original string. It is case-sensitive also.

const name = “Asif Ur Rahman”console.log(name.split(“ “)) // ["Asif","Ur","Rahman"]

10.startsWith()

It works with boolean. If one specified string starts with a given string, it will return true else it will return false.

const name = “Md Asif Ur Rahman”console.log(name.startsWith(“Md”)) // true

N.B: Upcoming article with substr, toLowercase, toUppercase, trim, trimStart, trimEnd.

--

--

Asif Ur Rahman

Hi ! I am Asif. A full time web developer. || Web Developer at OmniGo || MERN Stack Developer