JavaScript String slice()

Total
0
Shares

In this tutorial, we will learn about the JavaScript String slice() method with examples.

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

slice() Example

const text = "Hello World";

// Slice from index 0 to 5
console.log(text.slice(0,5));

// Slice from index 6 to end
console.log(text.slice(6));

Output

Hello
World

slice() Syntax

The syntax of the JavaScript slice() method is as follows.

str.slice(beginIndex, endIndex)

Here the str is the string or the string variable.

slice() Parameter

The JavaScript slice() method takes two parameters.

1) beginIndex – The starting index where the string slice should begin. 

If beginIndex is omitted, undefined, or cannot be converted to a number, the slice() method extracts from the beginning of the string.

If beginIndex is negative, slice() begins extraction from str.length + beginIndex. (E.g. "test".slice(-2) returns "st")

2) endIndex (Optional) – The ending index where the string slice should end. The character at this index will not be included. 

If endIndex is omitted, undefined, or cannot be converted to a number, the slice() method extracts till the end of the string.

If endIndex is negative, slice() is treated as str.length + endIndex. (E.g, if endIndex is -2, it is treated as str.length - 2 and "test".slice(1, -2) returns "e") .

slice() Return Value

The slice() method returns the new string, which contains the extracted section of the string based on the input parameters.

Example 1: Using JavaScript String slice() method

// JavaScript Program to illustrate slice() function

const text = "This is an example of String Slice Method";

// Slice from index 5 to end
console.log(text.slice(5));

// Slice from index 22 to index 34
console.log(text.slice(22,34));

Output

is an example of String Slice Method

String Slice

Example 2: Using slice() method with negative indices

 If beginIndex or endIndex is negative, slice() begins extraction from backwards. For example, -1 represents the last element, -2 represents the second last element and so on.

In our below example, the beingIndex is -12 and endIndex is -6. Hence it starts slicing backwards from the -12 index and ends with the -6 index.

const text = "This is an example of String Slice Method";

// Slice the last 6 characters
console.log(text.slice(-6));

// Slice from backwards index -12 to index -6
console.log(text.slice(-12, -6));

Output

Method
Slice 
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript Array slice()

JavaScript Array slice()

Table of Contents Hide slice() Exampleslice() Syntaxslice() Parametersslice() Return ValueExample 1: Clone an array using the JavaScript slice() methodExample 2: JavaScript slice() methodExample 3: JavaScript slice() With Negative indexExample 4:…
View Post