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

String.prototype.slice() β€” basic substring

πŸ“– 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()

str.slice(start, end) extracts a substring from index start up to (not including) end.

Challenge: Implement getMiddle(str) that returns characters from index 2 to 5 (exclusive).

getMiddle('abcdefgh') // β†’ 'cde'
πŸ’‘ Hints (1)
  • β€’ `str.slice(2, 5)` extracts characters at positions 2, 3, and 4.