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 of the best deals on our WordPress themes.

You May Also Like
JavaScript Array slice()

JavaScript Array slice()

The slice() method in JavaScript returns a shallow copy of a portion of an array as a new array. The original array remains unchanged. slice() Example slice() Syntax The syntax…
View Post
JavaScript String split()

JavaScript String split()

In this tutorial, we will learn about the JavaScript String split() method with examples. The JavaScript String split() method splits a string into an ordered list of strings and returns…
View Post
JavaScript String startsWith()

JavaScript String startsWith()

In JavaScript, the startsWith() method determines whether the given string begins with a specific sequence of characters. Let’s take a look at syntax and examples of the Javascript string startsWith()…
View Post
JavaScript String endsWith()

JavaScript String endsWith()

In JavaScript, the endsWith() method determines whether the given string ends with a specific sequence of characters. Let’s take a look at syntax and examples of the Javascript string endsWith()…
View Post
JavaScript String toUpperCase()

JavaScript String toUpperCase()

In this tutorial, we will learn about the JavaScript String toUpperCase() method with examples. The JavaScript String toUpperCase() method converts the string into uppercase and returns a new string. toUpperCase()…
View Post