What is a string in JavaScript?
A sequence of characters used to represent text, enclosed in single or double quotes.
How do you find the length of a string?
Use the .length property. Example: let size = str.length;
How do you convert a string to uppercase?
Use .toUpperCase(). Example: str.toUpperCase();
How do you convert a string to lowercase?
Use .toLowerCase(). Example: str.toLowerCase();
How do you extract a portion of a string using indices?
Use .slice(start, end). Example: str.slice(0, 5);
How do you replace part of a string?
Use .replace(searchValue, newValue). Example: str.replace('old', 'new');
How do you split a string into an array?
Use .split(separator). Example: 'a,b,c'.split(','); returns ['a', 'b', 'c']
How do you join an array into a string?
Use .join(separator). Example: ['a', 'b', 'c'].join('-'); returns 'a-b-c'
How do you remove whitespace from both ends of a string?
Use .trim(). Example: ' text '.trim();
How do you check if a string includes a substring?
Use .includes(substring). Example: str.includes('word');
How do you find the index of a substring?
Use .indexOf(substring). Example: str.indexOf('word'); returns index or -1.
How do you repeat a string multiple times?
Use .repeat(count). Example: 'ha'.repeat(3); returns 'hahaha'.
How do you check if a string starts with a substring?
Use .startsWith(substring). Example: str.startsWith('Hi');
How do you check if a string ends with a substring?
Use .endsWith(substring). Example: str.endsWith('end');
How do you concatenate two strings?
Use + or .concat(). Example: 'Hello' + ' World' or 'Hello'.concat(' World');
How do you convert other data types to a string?
Use String(value) or .toString(). Example: String(123);
How do you pad a string to a certain length at the start?
Use .padStart(targetLength, padString). Example: '5'.padStart(3, '0'); → '005'
How do you pad a string to a certain length at the end?
Use .padEnd(targetLength, padString). Example: '5'.padEnd(3, '0'); → '500'
How do you get a character at a specific index?
Use .charAt(index). Example: 'Hello'.charAt(1); → 'e'
How do you access a character using bracket notation?
Use str[index]. Example: 'Hello'[1]; → 'e'
What is the difference between slice, substring, and substr?
.slice() and .substring() are similar, but .substr() takes length instead of end index and is deprecated.
How do you check if two strings are equal?
Use === for strict comparison. Example: 'abc' === 'abc';
How do you escape special characters in a string?
Use backslash \. Example: 'It\'s ok';
How do you create a template literal?
Use backticks ` and ${} for interpolation. Example: `Hello ${name}`.