AppDividend
Latest Code Tutorials

Javascript Array Slice Example | Array.prototype.slice() Tutorial

1

=Javascript Array Slice Example | Array.prototype.slice() Tutorial is today’s leading topic. The slice()  method returns the shallow copy of the portion of an array into a new array object selected from beginning to end. Here in the result the end not included. The original array will not be modified. So it is a pure function.  The slice() method returns the selected elements in an array, as a new array object. The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript Array Slice Example

The syntax for javascript slice() method is following.

array.slice(start, end)

The start parameter is optional, and it is the integer that specifies where to start the selection.

The end parameter is optional, and it is the integer that specifies where to end the selection.

Let us take an example by creating a file called app.js and add the following code.

// app.js

let namepartner = ['Pearson', 'Specter', 'Litt'];
let suits = namepartner.slice(1, 2);

console.log(suits);

 

Javascript Array Slice Example | Array.prototype.slice() Tutorial

So here, what happens under the hood is that when the .slice() method is called frequently, this keyword works as an Array, and then it iterates over an Array.

So one question in your mind is that how this keyword in the .slice() function an Array? Because when you do.

object.method()

The object automatically refers to the value of this keyword in the javascript method(). So with:

[1,2,4,5].slice()

And the [1, 2, 4, 5] is an Array is set as the value of this in .slice() method.

Array-like objects

The slice() method can also be called to convert Array-like objects/collections into the new Array. You can bind a method to the object. The arguments inside the function are the example of the ‘array-like object.’

// app.js

function list() {
  return Array.prototype.slice.call(arguments);
}

const list1 = list(1, 2, 3);
console.log(list1);

We can create a bindin with the .call() function of the Function.prototype method and we can also reduced using [].slice.call(arguments) instead of Array.prototype.slice.call().

The output of the following code is this.

 

Javascript Array Slice Example

Let us take another example of slice() method in Javascript.

// app.js

let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep'];
let cut = months.slice(2, 5);

console.log(cut);

So, here the array index is starting from 0. So we will get an array object with the values of [‘Mar’, ‘Apr’, ‘May’].

At last, Javascript Array Slice Example | Array.prototype.slice() Tutorial is over.

1 Comment
  1. vero says

    I like your tutorials!

    But as someone who is only learning js, i’d like to point out that in this case is good to explain why the result is what it is.
    let namepartner = [‘Pearson’, ‘Specter’, ‘Litt’];
    let suits = namepartner.slice(1, 2);

    console.log(suits);
    Specter

    slice method with parameters 1 and 2 starts at 1 and end at 2(2 not included).
    so as arrays start count as 0, Pearson is ignored, Spencer with position 1 is the result and Litt with position 2 is also ignored.

    also maybe better example would be bigger array.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.