Categories
JavaScript Answers

How to get subarray from array with JavaScript?

Spread the love

To get subarray from array with JavaScript, we use the slice method.

For instance, we write

const ar = [1, 2, 3, 4, 5];
const ar2 = ar.slice(1, 4);

console.log(ar2);

to call ar.slice with 1 and 4 to return a new array with the entries in ar from index 1 to 3.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.