Five JavaScript string method you must know!

Kazi Minhajul Haider
2 min readMay 5, 2021
  1. charAt() method :
//syntax
charAt(index)

we can find a character from a string by calling charAt() method. You have to pass the integer index number between 0 to length-1. It will returns the character of that index. If given string is out of range then it will return empty string. if parameter is not passed then by default it return character at index number 0.

2. concat() method: concatenation is an important method. By using this method we can concat two string.

//syntax
concat(str1)
concat(str1, str2)
concat(str1, str2, ... , strN)

for example :

const str1 = 'kazi'
const str2 = 'minhaj'
const fullName = str1.concat(str2)
console.log(fullName) // kazi minhaj

3. includes() method: It is a case-sensitive method by which we can search a string into another string. It will returns true or false answer

//syntax 
includes(searchString)
includes(searchString, position)

for example:

const str= "I am good at JavaScript"
const searchStr = "good"
const result = str.includes(searchStr)
console.log(result) //true

4. endsWith() method: This method returns true or false result depending on a string ends with a specific string or not.

//syntax
endsWith(searchString)
endsWith(searchString, length) //length param is optional

for example :

const str1 = 'Shakib al hasan is the best player';console.log(str1.endsWith('best'));
// expected output: true
const str2 = 'Is Js is good?';console.log(str2.endsWith('?'));
// expected output: false

5. indexOf() method: The indexOf() method returns the index within the calling String object of the first occurence of the specified value

syntax:

indexOf(searchValue)
indexOf(searchValue, fromIndex)

example

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

--

--