Strings Flashcards

To learn about string manipulation and methods (25 cards)

1
Q

What is a string in JavaScript?

A

A sequence of characters used to represent text, enclosed in single or double quotes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you find the length of a string?

A

Use the .length property. Example: let size = str.length;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you convert a string to uppercase?

A

Use .toUpperCase(). Example: str.toUpperCase();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you convert a string to lowercase?

A

Use .toLowerCase(). Example: str.toLowerCase();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you extract a portion of a string using indices?

A

Use .slice(start, end). Example: str.slice(0, 5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you replace part of a string?

A

Use .replace(searchValue, newValue). Example: str.replace('old', 'new');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you split a string into an array?

A

Use .split(separator). Example: 'a,b,c'.split(','); returns ['a', 'b', 'c']

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you join an array into a string?

A

Use .join(separator). Example: ['a', 'b', 'c'].join('-'); returns 'a-b-c'

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you remove whitespace from both ends of a string?

A

Use .trim(). Example: ' text '.trim();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you check if a string includes a substring?

A

Use .includes(substring). Example: str.includes('word');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you find the index of a substring?

A

Use .indexOf(substring). Example: str.indexOf('word'); returns index or -1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you repeat a string multiple times?

A

Use .repeat(count). Example: 'ha'.repeat(3); returns 'hahaha'.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you check if a string starts with a substring?

A

Use .startsWith(substring). Example: str.startsWith('Hi');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you check if a string ends with a substring?

A

Use .endsWith(substring). Example: str.endsWith('end');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you concatenate two strings?

A

Use + or .concat(). Example: 'Hello' + ' World' or 'Hello'.concat(' World');

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you convert other data types to a string?

A

Use String(value) or .toString(). Example: String(123);

17
Q

How do you pad a string to a certain length at the start?

A

Use .padStart(targetLength, padString). Example: '5'.padStart(3, '0');'005'

18
Q

How do you pad a string to a certain length at the end?

A

Use .padEnd(targetLength, padString). Example: '5'.padEnd(3, '0');'500'

19
Q

How do you get a character at a specific index?

A

Use .charAt(index). Example: 'Hello'.charAt(1);'e'

20
Q

How do you access a character using bracket notation?

A

Use str[index]. Example: 'Hello'[1];'e'

21
Q

What is the difference between slice, substring, and substr?

A

.slice() and .substring() are similar, but .substr() takes length instead of end index and is deprecated.

22
Q

How do you check if two strings are equal?

A

Use === for strict comparison. Example: 'abc' === 'abc';

23
Q

How do you escape special characters in a string?

A

Use backslash \. Example: 'It\'s ok';

24
Q

How do you create a template literal?

A

Use backticks ` and ${} for interpolation. Example: `Hello ${name}`.

25
How do you match a pattern in a string using regex?
Use `.match(regex)`. Example: `'hello'.match(/h/);`