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

String.prototype.slice() β€” negative start (last N chars)

πŸ“– 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() β€” Negative Start

A negative start counts from the end of the string. str.slice(-3) gives the last 3 characters.

Challenge: Implement lastThree(str) that returns the last 3 characters.

lastThree('abcdef') // β†’ 'def'
lastThree('hi')     // β†’ 'hi'
πŸ’‘ Hints (1)
  • β€’ `str.slice(-3)` is equivalent to `str.slice(str.length - 3)`.