Take a look at Array.slice(begin, end)

const ar = [1, 2, 3, 4, 5];

// slice from 1..3 - add 1 as the end index is not included

const ar2 = ar.slice(1, 3 + 1);

console.log(ar2); // -> [2, 3, 4]
// the original ar is unmodified.
console.log(ar); // -> [1, 2, 3, 4, 5]
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Alex K. on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › subarray
TypedArray.prototype.subarray() - JavaScript - MDN Web Docs
If either begin or end is negative, it refers to an index from the end of the array instead of from the beginning. Also note that this is creating a new view on the existing buffer; changes to the new object's contents will impact the original object and vice versa. ... const buffer = new ArrayBuffer(8); const uint8 = new Uint8Array(buffer); uint8.set([1, 2, 3]); console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ] const sub = uint8.subarray(0, 4); console.log(sub); // Uint8Array [ 1, 2, 3, 0 ]
Discussions

How to set subarray elements?
When I run it, it throws an error “Uncaught TypeError: Cannot set property ‘0’ of undefined”. You can do this when the array is only 1d, then why not 2d arrays · Your secondArr is empty. Javascript does not know that you want that to be an array of arrays, so it throws an error when ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
0
January 6, 2021
javascript - Creating subarrays in array - Stack Overflow
I'm attempting to create subarrays within an array that contains groupings of like numbers. I'm coding in JavaScript. My array is [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20]; I've sorted the array ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to create sub array from array - Stack Overflow
I have large data structure to manipulate and I have some problems sorting similar data into sub array. var arr = [2016-12-16, 2016-12-16, 2016-12-17, 2016-12-17, 2016-12-17, 2016-12-17, 2016-12-18, More on stackoverflow.com
🌐 stackoverflow.com
How do I remove from an array those elements that exists in another array?
All you need is a simple filter function, or you could just manually loop over the array. var filteredPeople = people.filter(function(element) { return peopleToRemove.indexOf(element) === -1; }); More on reddit.com
🌐 r/javascript
11
8
October 10, 2014
🌐
Medium
medium.com › @murat.bilal › subarrays-in-javascript-d523f0d83145
subarrays in javascript
Read writing from Murat Bilal on Medium. Engineering, network automation, PostgreSQL, Linux and Programming. https://www.linkedin.com/in/murat-bilal-131bb91a2/
🌐
YouTube
youtube.com › watch
Create subarray from an array in Javascript - YouTube
We will see, how we can create a subarray from an array in Javascript. We will use the slice() function to create a new substring. What are inputs in slice ...
Published: October 7, 2022
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-generate-all-subarrays-in-javascript-in-2022-ef830d7ad885
How to Generate All Subarrays in JavaScript (2022) | by Ajay Verma | JavaScript in Plain English
November 2, 2022 - Join Medium for free to get updates from this writer. ... we will see how to generate all the subarrays of an array in javascript using iterative and recursive ways both.
🌐
DevGenius
blog.devgenius.io › how-to-get-a-subarray-from-a-javascript-array-ecdb73d0f0e6
How to Get a Subarray from a JavaScript Array? | by John Au-Yeung | Dev Genius
July 16, 2021 - Negative indexes mean we start counting from the end of the array instead of the start. So -1 is the last element of the array, -2 is the 2nd last one, and so on. ... We can use the JavaScript array’s slice method to extract a subarray from a JavaScript array.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-an-array-into-array-of-subarrays-using-javascript
Convert an Array into Array of Subarrays using JavaScript - GeeksforGeeks
July 23, 2025 - Space complexity: O(n), as each recursive call creates a new subarray · The map() method in JavaScript creates a new array by applying a function to each element of the original array.
Find elsewhere
🌐
W3Schools
w3schools.com › Jsref › jsref_typed_subarray.asp
JavaScript Typed Array subarray() Method
// Create a Typed Array const myArr = Int32Array.of(40, 100, 1, 5, 25, 10); // Create a Subarray subArr = myArr.subarray(2); Try it Yourself »
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-typedarray-subarray-with-examples
JavaScript typedArray.subarray() with Examples | GeeksforGeeks
December 27, 2023 - It returns a new array that is formed from the given typedArray object. Example 1: This example shows the basic use of the typedArray.subarray() function. Here it returns the value of the subarray with the given parameters.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript subarray()
JavaScript subarray() | How JavaScript subarray() works with Examples
April 1, 2023 - Then “C1” variable is created to store the array values from the index value “1”. Then “D1” variable is created to store the array values from the array index value “3”. Likewise “E1” variable is created to store the array values from the index values 0 and 6. Then “F1” variable is created to store the array values starting from the index value “0”. Like this, all the different subarrays are created and stored in different variables.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
YouTube
youtube.com › watch
how to get subarray from array in javascript - YouTube
Get Free GPT4o with 1 million code snippet from https://codegive.com to get a subarray from an array in javascript, you can use the `slice()` method. this m...
Published: June 15, 2024
🌐
TutorialsPoint
tutorialspoint.com › convert-array-into-array-of-subarrays-javascript
Convert Array into Array of Subarrays in JavaScript
October 1, 2020 - We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly
🌐
freeCodeCamp
forum.freecodecamp.org › t › how-to-set-subarray-elements › 439079
How to set subarray elements? - The freeCodeCamp Forum
January 6, 2021 - When I run it, it throws an error “Uncaught TypeError: Cannot set property ‘0’ of undefined”. You can do this when the array is only 1d, then why not 2d arrays · Your secondArr is empty. Javascript does not know that you want that to be an array of arrays, so it throws an error when ...
🌐
xjavascript
xjavascript.com › blog › how-to-get-subarray-from-array
How to Get a Subarray from an Array: Create a getSubarray Function (fromIndex to toIndex) — xjavascript.com
A subarray is a new array; it does not modify the original array (immutable). ... Return a new array containing elements from fromIndex to toIndex - 1. Handle edge cases like negative indices, out-of-bounds values, and invalid inputs. Be immutable: The original array remains unmodified. Let’s break down the logic of getSubarray into actionable steps. First, ensure the input is valid. The function expects the first argument to be an array.
🌐
The Web Dev
thewebdev.info › home › how to get a subarray from a javascript array?
How to Get a Subarray from a JavaScript Array? - The Web Dev
March 21, 2021 - Negative indexes mean we start counting from the end of the array instead of the start. So -1 is the last element of the array, -2 is the 2nd last one, and so on. ... We can use the JavaScript array’s slice method to extract a subarray from a JavaScript array.
🌐
ZetCode
zetcode.com › js-array › subarray
JavaScript subarray - extracting array portions in JS
April 4, 2025 - Negative indices count from the end of the array. The following example demonstrates the basic usage of the subarray method with a typed array. ... const buffer = new ArrayBuffer(8); const arr = new Int8Array(buffer); arr.set([10, 20, 30, 40, 50, 60, 70, 80]); const sub = arr.subarray(2, 5); console.log('Original:', arr); console.log('Subarray:', sub); We create an Int8Array and extract elements from index 2 to 4 (5 is excluded).
🌐
GitHub
gist.github.com › baybatu › 5663f238534290d15be7
Splitting array into list of subarrays in javascript · GitHub
sp_array = [1,2,3,4,5,6,7,8,9,10] let pivot = _.ceil(sp_array.length / 4); array_chunk = _.chunk(sp_array, pivot);
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › consecutive element subarrays
Generate all consecutive element subarrays from a JavaScript array - 30 seconds of code
March 20, 2024 - This is a fairly uncommon problem, yet it can be solved with a simple function. All you need to do is use Array.prototype.map() in order to map each element to an array of n consecutive elements, using Array.prototype.slice() to extract the subarray.