JavaScript Strings 2 — Questions and Answers
Question 1: What does `String.prototype.padStart()` do?
- Adds spaces to the end of a string
- Pads the beginning of a string with a specified string until it reaches a target length (Correct answer)
- Trims the start of a string
- Left-aligns the string content
Correct answer: Pads the beginning of a string with a specified string until it reaches a target length
`padStart(targetLength, padString)` pads the beginning of the current string with a given string (repeated if needed) so that the resulting string reaches a given length.
`'5'.padStart(3, '0')` → `'005'`. `'hi'.padStart(5)` → `' hi'` (space is default). If the target length is less than or equal to the string's length, no padding is added. Common use: formatting hours/minutes (`hours.toString().padStart(2, '0')`). `padEnd()` does the same from the right.
Question 2: What does `String.prototype.repeat()` do?
- Repeats the string until a target length is reached
- Returns a new string consisting of the original string repeated a specified number of times (Correct answer)
- Duplicates each character in the string
- Loops over each character of the string
Correct answer: Returns a new string consisting of the original string repeated a specified number of times
`repeat(count)` constructs and returns a new string containing the specified number of copies of the string it is called on, concatenated together.
`'ha'.repeat(3)` → `'hahaha'`. `'x'.repeat(0)` → `''`. Negative values or Infinity throw a `RangeError`. Useful for generating placeholder strings, creating visual separators in console output, or building test data. Not to be confused with `padStart`/`padEnd` which pad to a target length.
Question 3: What does `String.prototype.matchAll()` return?
- An array of all matches
- An iterator of all regex match results (including capture groups) (Correct answer)
- A boolean indicating if all patterns match
- The last match in the string
Correct answer: An iterator of all regex match results (including capture groups)
`matchAll(regexp)` returns an iterator of all results matching the regex (which must have the `g` flag), including capture groups. Unlike `match()`, each result includes full match information.
`match(/regex/g)` returns array of matched strings (no capture groups). `matchAll(/regex/g)` returns an iterator where each item is a full match object with `[0]` (full match), capture groups, `.index`, and `.input`. This is useful for extracting all matches with their capture groups. The regex MUST have the `g` flag or a TypeError is thrown.
Question 4: What does `String.prototype.startsWith()` do?
- Returns the first character of the string
- Determines whether a string begins with the characters of a specified string (Correct answer)
- Checks if the string starts with a uppercase letter
- Returns a substring from the start
Correct answer: Determines whether a string begins with the characters of a specified string
`startsWith(searchString, position)` returns `true` if the string begins with the specified characters (starting at the given position, default 0), otherwise `false`. It's case-sensitive.
`'hello world'.startsWith('hello')` → `true`. `'hello world'.startsWith('world', 6)` → `true` (checks starting at position 6). Case-sensitive: `'Hello'.startsWith('hello')` → `false`. Used for checking URL protocols (`url.startsWith('https://')`), file extensions, etc. More readable than `indexOf(str) === 0`.
Question 5: What does the `String.raw` tagged template do?
- Converts all characters to uppercase
- Returns the raw string form of a template literal, without processing escape sequences (Correct answer)
- Removes all whitespace from the string
- Encrypts the string content
Correct answer: Returns the raw string form of a template literal, without processing escape sequences
`String.raw` is a tag function for template literals that returns a string where escape sequences (like `\n`, `\t`) are NOT processed — you get the raw characters instead.
`String.raw`\n\t`` → `'\n\t'` (literal backslash-n and backslash-t, not newline and tab). `String.raw`C:\Users\file`` → `'C:\\Users\\file'`. Particularly useful for regular expressions in template literals and Windows file paths where you'd otherwise need to double all backslashes.
Question 6: What does `String.prototype.at()` do?
- Returns the character at the given index, supporting negative indices (Correct answer)
- Returns the Unicode code point at the given position
- Checks if a character is at a given position
- Finds the index of a character
Correct answer: Returns the character at the given index, supporting negative indices
`at(index)` returns the character at the specified index, supporting negative integers to count from the end. `str.at(-1)` returns the last character.
`'hello'.at(0)` → `'h'`. `'hello'.at(-1)` → `'o'` (last character). `'hello'.at(-2)` → `'l'`. Unlike `str[index]` which returns `undefined` for negative indices (since object property names can't be negative), `at()` properly handles negatives. Introduced in ES2022, also available on arrays and TypedArrays.
What does `String.prototype.padStart()` do?