New String Methods Flashcards Preview

Javascript ES6 > New String Methods > Flashcards

Flashcards in New String Methods Deck (8)
Loading flashcards...
1
Q

What method allows you to see if a string begins with a certain string?

A
var string = 'RFB2';
string.startsWith('RFB')
> true!
2
Q

Would startWidth allow for case sensitivity?

A

No

3
Q

What does the second param of startsWith allow you to do?

A
Starts after X characters
const string = 'RFB2';
string.startsWith('RFB', 3)
> true!
4
Q

What method allows you to see if a string ends with a certain string?

A
const string = '2-AC-2018-JZ';
string.endsWith('JZ')
> true!
5
Q

How would you selectively use endsWith to ignore the last few characters of a string

A

Use the second parameter to choose how many characters from the start of the string you want to check:

const string = ‘ADGDRT001’;

string.endsWidth(‘RT’,6);

6
Q

What method would you use to check for the presence of ‘ac’ in a string?

A

.includes(‘ac’);

7
Q

What method allows you to repeat a string?

A

.repeat(‘string’);

8
Q

What would .repeat() be useful. Give an example.

A

to format text in a left pad function.

function leftPad(str, length = 20) {
return `- ${ ' '.repeat(length - str.length) }`;
}