JavaScript Strings 1 — Questions and Answers
Question 1: What do template literals (template strings) allow in JavaScript?
- Only multi-line strings
- String interpolation with `${}` and multi-line strings using backticks (Correct answer)
- Automatic string escaping
- Importing HTML templates
Correct answer: String interpolation with `${}` and multi-line strings using backticks
Template literals, enclosed in backticks (`), allow embedded expressions using `${expression}`, and support multi-line strings directly without escape sequences.
Template literals enable: expression interpolation (`Hello, ${name}!`), multi-line strings (newlines are preserved), and tagged templates (function that processes the literal). They're the modern replacement for string concatenation with `+`. Tagged templates power libraries like styled-components and graphql-tag.
Question 2: What does `String.prototype.trim()` do?
- Removes all whitespace from the string
- Removes whitespace from the beginning and end of a string (Correct answer)
- Replaces multiple spaces with single spaces
- Limits the string to a maximum length
Correct answer: Removes whitespace from the beginning and end of a string
`trim()` removes leading and trailing whitespace (spaces, tabs, newlines) from a string. It does not modify whitespace in the middle of the string. `trimStart()` and `trimEnd()` target only one side.
`' hello world '.trim()` → `'hello world'`. `trimStart()` (or `trimLeft()`) removes only leading whitespace. `trimEnd()` (or `trimRight()`) removes only trailing. These are commonly used when processing user input to avoid bugs caused by accidental spaces. All three return a new string.
Question 3: What does `String.prototype.split()` return?
- A string with the separator removed
- An array of substrings divided by the separator (Correct answer)
- A single character at the split position
- A Set of unique parts
Correct answer: An array of substrings divided by the separator
`split(separator)` divides a string into an ordered array of substrings by searching for the separator, then returns the array.
`'a,b,c'.split(',')` → `['a', 'b', 'c']`. `'hello'.split('')` → `['h','e','l','l','o']`. `split()` with no argument returns `['entire string']`. The second argument limits the number of splits: `'a,b,c'.split(',', 2)` → `['a', 'b']`. You can also split by regex: `'one two'.split(/\s+/)` → `['one', 'two']`.
Question 4: What does `String.prototype.includes()` do?
- Returns the index of the substring
- Returns true if the string contains the given substring, otherwise false (Correct answer)
- Adds a substring to the string
- Replaces a substring with another
Correct answer: Returns true if the string contains the given substring, otherwise false
`includes(searchString, position)` determines whether one string can be found within another, returning `true` or `false`. It performs a case-sensitive search.
`'hello world'.includes('world')` → `true`. The optional second argument specifies the starting position: `'hello'.includes('ell', 2)` → `false` (search starts at index 2). Case-sensitive: `'Hello'.includes('hello')` → `false`. For case-insensitive search: `str.toLowerCase().includes(term.toLowerCase())`.
Question 5: What does `String.prototype.replace()` do?
- Replaces ALL occurrences of a string or regex pattern
- Replaces the first occurrence of a pattern (string or regex without `g` flag) with a replacement (Correct answer)
- Removes a substring from the string
- Replaces characters by position
Correct answer: Replaces the first occurrence of a pattern (string or regex without `g` flag) with a replacement
`replace(pattern, replacement)` returns a new string with the first match replaced. To replace all occurrences with a string pattern, use `replaceAll()` or a regex with the `g` flag.
`'aabbcc'.replace('b', 'X')` → `'aaXbcc'` (only first). `'aabbcc'.replace(/b/g, 'X')` → `'aaXXcc'` (all). ES2021 added `replaceAll()` for string patterns without needing a regex. The replacement can be a string (with `$&` for matched text, `$1` for capture groups) or a function that receives the match and returns the replacement.
Question 6: What is the difference between `String.prototype.slice()` and `String.prototype.substring()`?
- They are identical
- `slice()` accepts negative indices; `substring()` treats negatives as 0 (Correct answer)
- `substring()` accepts negative indices; `slice()` does not
- `slice()` is for arrays; `substring()` is for strings
Correct answer: `slice()` accepts negative indices; `substring()` treats negatives as 0
Both extract a portion of a string, but `slice()` supports negative indices (counting from the end), while `substring()` treats negative values as `0`. Also, if start > end, `substring()` swaps them, while `slice()` returns empty string.
`'hello'.slice(-3)` → `'llo'` (last 3 chars). `'hello'.substring(-3)` → `'hello'` (negatives become 0). `'hello'.slice(3, 1)` → `''` (start > end, returns empty). `'hello'.substring(3, 1)` → `'el'` (swaps to substring(1, 3)). `slice()` is generally preferred over `substring()` for its consistent, predictable behavior with negative indices.
What do template literals (template strings) allow in JavaScript?