Z anika Nibir
3 min readMay 5, 2021

--

#10 Example of JavaScript String

1.chartAt: It is a method of javaScript string.The method returns a specified index in a string. The Index value starts from 0.

Example{

const speech = ‘This article is about chartAt’;

console.log(“It is”+speech.charAt(0));

Output: It is T

}

2.concat: This javascript method stops the string arguments to the calling string and returns a new string.

Example{

const sentence1 = ‘Hate’;

const sentence2 = ‘You’;

console.log(sentence1.concat(‘ ‘, sentence2));

Output: “Hate You”

}

3.includes: This method performs a case-sensitive.Its helps to search to determine if one string may be found within another string.Then in returns true or false what is appropriate

Example{

const sentence = His pet is a dog;

const word = ‘cat’;

console.log(`His pet is a “${word}” ${sentence.includes(word) ? ‘is’ : ‘is not’}`);

Output: “His pet is a dog”

}

4. endsWith:This method detaches a string ends with the characters of a specified string and ith returns true or false as appropriate.

Example{

const sample1 = ‘He loves to eat’;

console.log(str1.endsWith(‘eat’, 15));

Output: true

}

5.indexOf: The method returns the index within the calling string.object of the first occurrence of the specified value, starting the search at formindex. if the value is not found then it returns -1.

Example{

const sentence = ‘My mom is the best chef in the world’;

const search = ‘world’;

const indexOfFirst = sentence.indexOf(search);

console.log(indexOfFirst);

Output: 31

}

6. lastIndexOf: The method returns the index with the specific string that we called.object of the last occurrence of the specified value, searching backwards from fromindex. if the value is not found it returns -1.

Example{

const sentence = ‘My mom is the best chef in the world.My mom cooks meat very well’;

const search = ‘world’;

const indexOfFirst = sentence.indexOf(search);

console.log(indexOfFirst);

Output: 40

}

7.replace: The method returns a new string with some or all matches of a structure replaced by a replacement.And the replacement.can be a string or a function to be called for each match. If structure is a string, only the first occurrence will be replaced.

Example{

const sentence =’The tiger is faster than lion’;

console.log(p.replace(‘cheetahs’, ‘tiger’));

Output: ’The cheetahs is faster than lion’

}

8.slice: This method extracts a section of a string and returns it as a new string, without modifying the original string.

Example{

const sentence = His pet is a dog;

console.log(sentence.slice(13));

Output: “dog”}

9.splite The method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

Example{

const sentence = ‘The quick brown fox jumps over the lazy dog.’;

const words = sentence.split(‘ ‘);

console.log(words[3]);

Output: “fox”

}

10.startsWith:The method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

Example{

const sentence = ‘Saturday night plans’;

console.log(sentence.startsWith(‘Sat’));

Output: true

}

--

--