JS Built-ins
← String125 / 195
StringString.prototype.slicebeginner

String.prototype.slice() β€” slice single char

πŸ“– Usage Example
const str = 'Hello, World!'
str.slice(7, 12)    // β†’ 'World'
str.slice(-6, -1)   // β†’ 'World'
str.slice(7)        // β†’ 'World!'

Use slice() to extract a portion of a string by start and end indices, supporting negative indices.

String.prototype.slice() β€” Single Character

Use .slice(n, n+1) to extract exactly one character at index n.

Challenge: Implement charAt(str, n) that returns the character at index n using slice.

charAt('hello', 1) // β†’ 'e'
charAt('world', 4) // β†’ 'd'
πŸ’‘ Hints (1)
  • β€’ `str.slice(n, n + 1)` extracts exactly one character at index `n`.