JS String Methods Flashcards
String.prototype.at()
Takes an integer value and returns a new string consisting of the character located at the specified index.
Negative integers count back from the last string character
Syntax - at(index)
const sentence = 'The quick brown fox jumps over the lazy dog.'; console.log(`An index of 5 returns the character ${sentence.at(5)}`); // Expected output: "An index of 5 returns the character u"
String.prototype.charAt()
Returns a new string consisting of the character at the given index
Syntax - charAt(index)
const sentence = 'The quick brown fox jumps over the lazy dog.'; console.log(`The character at index 4 is ${sentence.charAt(4)}`); // Expected output: "The character at index 4 is q"
String.prototype.concat()
Concatenates the string arguments to this string and returns a new string
Syntax - concat(); concat(str1); concat(str1, str2); concat(str1, str2, /* …, */ strN)
const str1 = 'Hello'; const str2 = 'World'; console.log(str1.concat(' ', str2)); // Expected output: "Hello World"
String.prototype.endsWith()
Determines whether a string ends with the characters of this string, returning true or false as appropriate
Syntax - endsWith(searchString); endsWith(searchString, endPosition)
const str1 = 'Cats are the best!'; console.log(str1.endsWith('best!')); // Expected output: true console.log(str1.endsWith('best', 17)); // Expected output: true const str2 = 'Is this a question?'; console.log(str2.endsWith('question')); // Expected output: false
P
String.prototype.includes()
Performs a case-sensitive search to determine whether a given string may be found within this string, returning true or false as appropriate
includes(searchString); includes(searchString, position)
const sentence = 'The quick brown fox jumps over the lazy dog.'; const word = 'fox'; console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`); // Expected output: "The word "fox" is in the sentence"
S
String.prototype.indexOf()
Searches a string and returns the index of the first occurrence of the specified substring. Takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number
Syntax - indexOf(searchString); indexOf(searchString, position)
const paragraph = "I think Ruth's dog is cuter than your dog!"; const searchTerm = 'dog'; const indexOfFirst = paragraph.indexOf(searchTerm); console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`); // Expected output: "The index of the first "dog" is 15"
String.prototype.lastIndexOf()
Searches this string and returns the index of the last occurrence of the specified substring. It takes an optional starting position and returns the last occurrence of the specified substring at an index less than or equal to the specified number.
Syntax - lastIndexOf(searchString); lastIndexOf(searchString, position)
const paragraph = "I think Ruth's dog is cuter than your dog!"; const searchTerm = 'dog'; console.log(`Index of the last ${searchTerm} is ${paragraph.lastIndexOf(searchTerm)}`); // Expected output: "Index of the last "dog" is 38"