10 of Javascript String Methods, All you need to know.

Monsur Rana
3 min readMay 5, 2021
Js String methods

1.indexOf()

indexOf method is used for search for a word. If you needed to search a word from a string, you have to use the indexOf() method. for example

const paragraph = ‘The quick brown fox jumps over the lazy dog.’;
const searchFor = paragraph.indexOf(‘fox’);
console.log(searchFor)
//returns : 16;

2. includes()

includes() method is used for serch for a word from an string. If you needed to search for a word , is it on the string , you should use the includes() method.

const sentence = ‘Mohammad determined to be a web developer’;const word = ‘Web’;console.log(`The word “${word}” ${sentence.includes(word) ? ‘is’ : ‘is not’} in the sentence`);
// returns: “The word “Web” is not in the sentence”

3. endsWith()

endsWith() method is used to check a string whether finished with a specified sentence or punctuation. for example,

const string1 = ‘Javascript is an user friendly programming language’;const ends1 = string1.endsWith(‘language’);
console.log(ends1)
// expected output: true
const string2 = ‘Its an string’;const ends2 = string2.endsWith(‘.’)
console.log(ends2)
// expected output: false

4. lastIndexOf()

this method is used to find a word or key from an array that which position this was staying on. This method finds the word or key from the last side of the string. for example,

const paragraph = ‘A quick brown fox jumps over the lazy dog.’;const searchTerm = ‘dog’;
const search = paragraph.lastIndexOf(searchTerm);
console.log(search);
// expected output: “38”

5.replace()

This method is used to replace a specific word of a string. for example

const paragraph = ‘The quick brown fox jumps over the lazy dog.’;const replacing = paragraph.replace(‘fox’, ‘rabbits’)
console.log(replacing);
// expected output: “The quick brown rabbits jumps over the lazy dog.”

6.slice()

This method is used to extract a part of the string from the original string without changing the original string. for example

const paragraph = ‘The quick brown fox jumps over the lazy dog.’;const extracting = paragraph.slice(10,25);
console.log(extracting)
//expected output: “brown fox jumps”

7.toUpperCase()

This method is used to convert all letters to uppercase of a string. This method converts all types of cases to uppercase. for example

const sentence = ‘Javascript is a familiar programming language’;const uppercase = sentence.toUpperCase()
console.log(uppercase)
// expected output: “JAVASCRIPT IS A FAMILIAR PROGRAMMING LANGUAGE”

8.toLowerCase()

This method is used to convert all letters to the lowercase of a string. This method converts all types of cases to lowercase. for example

const paragraph = ‘javascript Is A Familiar Programming Language.’;const lowercase = paragraph.toLowerCase()
console.log(lowercase);
// expected output: “javascript is a familiar programming language.”

9.trim()

This method is used to remove whitespace from both first and end. If have any unexpected whitespace on the string, this method is used to fix them. for example

const sentence = ‘ Wellcome to my world! ‘;console.log(sentence);
// expected output:” Wellcome to my world! “;
const trimming = sentence.trim();
console.log(trimming);
// expected output: “Wellcome to my world!”;

10.charAt()

This method is used to returns a new string from the consistent string. this method returns the letter which was we try to find. for example

const sentence = ‘Javascript in a familiar programming language’;const index = 11;console.log(sentence.charAt(index));//expected output: “i”

--

--

Monsur Rana
0 Followers

the biggest risk is not take any risk