JS String Methods Flashcards

1
Q

String.prototype.at()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

String.prototype.charAt()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String.prototype.concat()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

String.prototype.endsWith()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

P

String.prototype.includes()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

S

String.prototype.indexOf()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String.prototype.lastIndexOf()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly