10 mostly commonly used string methods in JavaScript

Raihan kabir
2 min readNov 2, 2020

String is one of the most commonly used data type in any programming language. A JavaScript developer has to deal a lot with string and JavaScript has so many build-in methods to work with string. Among all of the methods the most commonly used methods are

  1. charAt()
  2. substring()
  3. concat()
  4. substr()
  5. includes()
  6. indexOf()
  7. split()
  8. slice()
  9. trim()
  10. toString()

charAt():

Syntax: charAt(index)

This method takes one parameter index and return the character of the given index position

const string = "Hello world"
string.charAt(6) // return "w"

substring():

Syntax : substring(start index, end index)

Some Rules for the parameters :

  • If endIndex is omitted, substring() extracts characters to the end of the string.
  • If startIndex is equal to endIndex , then returns an empty string.
  • If startIndex is greater than endIndex, then the two arguments were swapped;
const string = "Hey! i love JavaScript"
string.substring(4) // return "i love JavaScript"
string.substring(0,4) // return "Hey!"
string.substring(11,5) // return "i love"

concat():

Syntax : concat(srt1, str2,…, strN)

This method takes n numbers of string and concatenate them together.

const str1 = "I"
const str2 = "love"
const str2 = "fruits"
str1.concat(" ",str2," ",str3)
//output : "I love fruits"

includes():

Syntax : includes(search string, position)

Includes() method takes two parameter first one is the search string and second one is the position you want to start your search default value = 0 and return true/ false according to given input

const string = "i love JavaScript and Python."
string.includes("JavaScript") //true
string.includes("JavaScript",20) //false

substr():

Syntax : substring(start index, length)

it takes two parameters start index and length of sub string

const string = "Hey! i love JavaScript"
string.substring(5,4) // return "i lo"
string.substring(-1,6)//return "Script"negative value refer last

indexOf():

Syntax : indexof(str, position) position optional

indexOf() methods return the first occurrence of the search string or return -1 if does not match any

const str = "hello world"
str.indexOf("world") //return 7
str.indexOf("hi") // -1

split():

Syntax: split(separator, length) length is optional

split() takes separator and split the given string by the separator and return the array of the separate sub string. you can control the the number of sub string you want

const fruits = "Banana,Apple,Mango, Blackberry"
fruits.split(",") //return ["Banana","Apple", "mango","Blackberry"]
fruits.split("," , 2) //return ["Banana","Apple"]

slice()

Syntax: slice(start index, end index)

slice() methods work as similar as substring() but it can also work with negative index, it takes negative index from the last index

const str1= "hello world"
str1.slice(1, 8)// return "hello wo"
str1.slice(6, -2) // return "wor"

trim()

Syntax: trim()

this method remove the white space from start and end of a string.

const str = "  hello  "
str.trim() //return "hello"

toString()

Syntax: toString()

toString() method convert string object to string literal and also convrt other data type to string

const num = 15;
num.toString(); // return "15"
const str = new String("hello world")
str.toString() // return "hello world"

--

--

Raihan kabir
0 Followers

Software Engineer | Nodejs | React | Typescript