What is the Slice Method in JavaScript?


The slice() is a built-in method in JavaScript utilized for extracting a section of an array or a string and returns/outputs a new one. By default, this method returns the new string/array without modifying the original. However, it can also be applied to update that specific string/array permanently. The slice() method has multiple use cases, which strengthens this method, i.e., it creates a new array/string subset better than various string/array manipulation methods.

Considering its importance, this post will list the syntax, brief working, and usage examples of JavaScript’s slice() method.

Syntax of slice() Method in JavaScript

The primary functionality of the slice() method can be understood from the following syntax:

ARRAY/STRING.slice(start, end);

The ARRAY or the STRING is joined with the “slice” with the help of the “dot” operator. While the start and the end parameters specify the index from where the slicing starts and ends. Both of these parameters are optional.

How to Use the slice() Method in JavaScript?

As discussed, the slice method slices the string or an array in a defined range (start and ending indexes). If the start or end positions are not defined, the slice(0) creates a copy of the string/array. Let’s overview its detailed working through examples:

Example 1: Slicing an Array

Here, the following code interprets the usage of the slice() method on the Array:

Code:

var arr = [20, 40, 60, 80, 100, 120, 140, 160];
var SlArr = arr.slice(3, 6);
console.log(SlArr);

An array is initialized, and the “Slarr” variable stores the sliced value of the array starting from the 3rd index to the 6th index.

Output:

slice method output

The output shows that the input string has been sliced to only three indexes.

Example 2: Slicing a String

The provided example demonstrates the application of the slice() method to slice a string:

Code:

var str = "JavaScript Tutorial! This blog is about the slice() Method.";
var SlString = str.slice(4, 20);
console.log(SlString);

Here, the slicing is performed on the string starting from the 10th index till the 26th index.

Output:

slice method second output

The output has printed the characters falling between the 4th and the 20th index.

Example 3: Creating a Duplicate String/Array

If the slice() method’s start and end parameters are not defined, it creates a duplicate array. This scenario is demonstrated using the code:

Code:

var arr = [2, 4, 6, 8, 10, 12];
var SlArr = arr.slice();
console.log(SlArr);

Output:

slice method third output

The output shows that the original array has been printed.

Wrap-Up

The slice() method is used to get the specific part of the string or an array to perform any operation on that part. By default, it does not manipulate the original array. However, if the user wants to update the original string/array, they can replace the result with the original string.
This post has briefly explained the brief working and usage of the slice() method in JavaScript.

You can get more JavaScript tutorials by following Linux Genie.

Print Friendly, PDF & Email
Categories