Top 10 string method in JavaScript you should know

1. charAt():
chartAt() means character at. It returns the character of a index number.
const sentence = 'I love my country';console.log(`The character at index 3 is ${sentence.charAt(3)}`);Output:
"The character at index 3 is o"
2. concat():
concat() method concatenates two or more strings together.
const string1 = 'Mashrafe';
const string2 = 'Mortaza';const addStrings = string1.concat(' ',string2);
console.log(addStrings);Output: "Mashrafe Mortaza"const newString = string2.concat(' ',string1);
console.log(newString);Output: "Mortaza Mashrafe"
3. includes():
This method returns true or false value by case-sensative searching to detect one string in another string.
const sentence = 'I love to play cricket';const word = 'play';console.log(sentence.includes(word));Output: trueconst word2 = 'box';
console.log(sentence.includes(word2));Output: false
4. indexOf():
indexOf() method returns the index number of an string in another string. If the searched string is not found then it returns -1.
const string = "Let's learn string of javascript";const searchString = 'learn';console.log(string.indexOf(searchString));Output: 6const searchString2 = 'earn';
console.log(string.indexOf(searchString2))Output: -1
5. replace():
The method replace a string by searching into another string. If the searching string doesn’t match then the main string remain same.
const string = "This is mission escape. Are you ready?";
console.log(string.replace('escape', 'run'));Output:
"This is mission run. Are you ready?"
6. slice():
The method does great job. It bring out a section of a string and returns it as a new string. The original string doesn’t modify at all.
const string = 'I am a javascript developer and I love string';
console.log(string.slice(7));
Output:
"javascript developer and I love string"console.log(string.slice(5, 11));
Output:
"a java"console.log(string.slice(-6));
Output:
"string"console.log(string.slice(-13, -7));
Output:
"I love"
7. split():
The method brings out an ordered list of substrings from a string, puts these substrings into an array, and returns the array.
const string = 'I am a javascript developer and I love string';const words = string.split(' ');
console.log(words[5]);Output:
"and"
8. substr():
The method returns a part of the string, starting at the specified index and pervading for a given number of characters afterwards.
const string = 'Mashrafe';
console.log(str.substr(2, 5));Output:
"shraf"
9. toLowerCase():
It’s a very fun fact that this method convert all uppercase word to lowercase.
const sentence = 'I am a javascript developer and I love string';
console.log(sentence.toLowerCase());Output:
"i am a javascript developer and i love string"
10. trim():
This method does a cool fact. It removes whitespace from both ends of a string.
const sentence = ' I am a javascript developer and I love string ';
console.log(sentence.trim());Output:
"I am a javascript developer and I love string"