JavaScript Code

JavaScript String slice()


String slice()

slice(start)
slice(start, end)
  • start is a number
  • end is a number

slice() method slices the given string from given start, to end position, and returns the sliced string.

Examples

1. slice(start)

let name = 'appleisgreat';
let output = name.slice(6);
console.log(output);

2. slice(start) with start as negative number

let name = 'appleisgreat';
let output = name.slice(-8); //negative index means counting from end
console.log(output);

3. slice(start, end)

let name = 'appleisgreat';
let output = name.slice(4, 9);
console.log(output);

Copyright @2022