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

String.prototype.slice() β€” omit end to end-of-string

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

If you omit the second argument, .slice() goes to the end of the string.

Challenge: Implement fromIndex(str, n) that returns everything from index n onwards.

fromIndex('hello world', 6) // β†’ 'world'
fromIndex('abcdef', 3)      // β†’ 'def'
πŸ’‘ Hints (1)
  • β€’ `str.slice(n)` without a second argument is equivalent to `str.slice(n, str.length)`.