JavaScript String slice()
Syntax & Examples


String.slice() method

The slice() method of the String class in JavaScript extracts a section of a string and returns it as a new string, without modifying the original string.


Syntax of String.slice()

There are 2 variations for the syntax of String.slice() method. They are:

1.
slice(indexStart)

Parameters

ParameterOptional/RequiredDescription
indexStartrequiredThe zero-based index at which to begin extraction. A negative index can be used, indicating an offset from the end of the string.

This method extracts a section of a string starting from the given indexStart to the end of the string.

Returns value of type String.

2.
slice(indexStart, indexEnd)

Parameters

ParameterOptional/RequiredDescription
indexStartrequiredThe zero-based index at which to begin extraction. A negative index can be used, indicating an offset from the end of the string.
indexEndoptionalThe zero-based index before which to end extraction. The character at this index will not be included. If omitted, extraction continues to the end of the string. A negative index can be used, indicating an offset from the end of the string.

This method extracts a section of a string starting from the given indexStart up to, but not including, the specified indexEnd.

Returns value of type String.



✐ Examples

1 Using slice() method with only start index

In JavaScript, we can use the slice() method to extract a section of a string starting from the specified index to the end of the string.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the slice() method with the start index 7.
  3. The extracted section, 'world!', is stored in the variable slicedStr.
  4. We log slicedStr to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const slicedStr = str.slice(7);
console.log(slicedStr);

Output

'world!'

2 Using slice() method with start and end indices

In this example, we use the slice() method to extract a section of a string starting from the specified start index and ending at, but not including, the specified end index.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the slice() method with the start index 0 and the end index 5.
  3. The extracted section, 'Hello', is stored in the variable slicedStr.
  4. We log slicedStr to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const slicedStr = str.slice(0, 5);
console.log(slicedStr);

Output

'Hello'

3 Using slice() method with negative indices

This example demonstrates how to use the slice() method with negative indices to extract a section of a string from the end.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the slice() method with the start index -6 to extract the last six characters.
  3. The extracted section, 'world!', is stored in the variable slicedStr.
  4. We log slicedStr to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const slicedStr = str.slice(-6);
console.log(slicedStr);

Output

'world!'

Summary

In this JavaScript tutorial, we learned about slice() method of String: the syntax and few working examples with output and detailed explanation for each example.