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

String.prototype.slice() β€” negative end

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

A negative end counts from the end. str.slice(0, -1) drops the last character.

Challenge: Implement dropLast(str) that removes the last character.

dropLast('hello') // β†’ 'hell'
dropLast('ab')    // β†’ 'a'
πŸ’‘ Hints (1)
  • β€’ `str.slice(0, -1)` is equivalent to `str.slice(0, str.length - 1)`.