In ES6 using Array from() and keys() methods.

Array.from(Array(10).keys())
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Shorter version using spread operator.

[...Array(10).keys()]
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Start from 1 by passing map function to Array from(), with an object with a length property:

Array.from({length: 10}, (_, i) => i + 1)
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Answer from Niko Ruotsalainen on Stack Overflow
Top answer
1 of 16
3985

In ES6 using Array from() and keys() methods.

Array.from(Array(10).keys())
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Shorter version using spread operator.

[...Array(10).keys()]
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Start from 1 by passing map function to Array from(), with an object with a length property:

Array.from({length: 10}, (_, i) => i + 1)
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2 of 16
901

You can do so:

var N = 10; 
Array.apply(null, {length: N}).map(Number.call, Number)

result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

or with random values:

Array.apply(null, {length: N}).map(Function.call, Math.random)

result: [0.7082694901619107, 0.9572225909214467, 0.8586748542729765, 0.8653848143294454, 0.008339877473190427, 0.9911756622605026, 0.8133423360995948, 0.8377588465809822, 0.5577575915958732, 0.16363654541783035]

Explanation

First, note that Number.call(undefined, N) is equivalent to Number(N), which just returns N. We'll use that fact later.

Array.apply(null, [undefined, undefined, undefined]) is equivalent to Array(undefined, undefined, undefined), which produces a three-element array and assigns undefined to each element.

How can you generalize that to N elements? Consider how Array() works, which goes something like this:

function Array() {
    if ( arguments.length == 1 &&
         'number' === typeof arguments[0] &&
         arguments[0] >= 0 && arguments &&
         arguments[0] < 1 << 32 ) {
        return [ … ];  // array of length arguments[0], generated by native code
    }
    var a = [];
    for (var i = 0; i < arguments.length; i++) {
        a.push(arguments[i]);
    }
    return a;
}

Since ECMAScript 5, Function.prototype.apply(thisArg, argsArray) also accepts a duck-typed array-like object as its second parameter. If we invoke Array.apply(null, { length: N }), then it will execute

function Array() {
    var a = [];
    for (var i = 0; i < /* arguments.length = */ N; i++) {
        a.push(/* arguments[i] = */ undefined);
    }
    return a;
}

Now we have an N-element array, with each element set to undefined. When we call .map(callback, thisArg) on it, each element will be set to the result of callback.call(thisArg, element, index, array). Therefore, [undefined, undefined, …, undefined].map(Number.call, Number) would map each element to (Number.call).call(Number, undefined, index, array), which is the same as Number.call(undefined, index, array), which, as we observed earlier, evaluates to index. That completes the array whose elements are the same as their index.

Why go through the trouble of Array.apply(null, {length: N}) instead of just Array(N)? After all, both expressions would result an an N-element array of undefined elements. The difference is that in the former expression, each element is explicitly set to undefined, whereas in the latter, each element was never set. According to the documentation of .map():

callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

Therefore, Array(N) is insufficient; Array(N).map(Number.call, Number) would result in an uninitialized array of length N.

Compatibility

Since this technique relies on behaviour of Function.prototype.apply() specified in ECMAScript 5, it will not work in pre-ECMAScript 5 browsers such as Chrome 14 and Internet Explorer 9.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-create-an-array-containing-1-n-numbers-in-javascript
JavaScript - Create an Array Containing 1...N Numbers - GeeksforGeeks
July 23, 2025 - Uses a for loop to iterate from 1 to N, adding each number to newArr with push(). Returns the array. We can also use Spread Operator to create an array containing 1...N numbers.
Discussions

How do you make a deep copy of an array in Javascript?
To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
6
1
June 22, 2024
How do I create an array, with every integer between 1 and 10000 inclusive, without using a loop?
int a[10000] = {0}; a[0] = 1; a[1] = 2; … a[9998] = 9999; a[9999] = 10000; More on reddit.com
🌐 r/C_Programming
55
8
July 5, 2023
🌐
Polvara
polvara.me › posts › create-an-array-containing-1-to-N-in-JS
Creating an Array Containing Values 1 to N in JavaScript
But that's not all! We can also pass a map function as a second argument: Array.from({ length: 3 }, (_, i) => i + 1); // [1, 2, 3]
🌐
Techie Delight
techiedelight.com › home › java › create an array sequence from 1 to n in a single line in javascript
Create an array sequence from 1 to N in a single line in JavaScript | Techie Delight
July 7, 2026 - There are several ways to create an array sequence of integers from 1 (inclusive) to `N` (inclusive), where the value `N` is dynamic.
🌐
DEV Community
dev.to › shafiemoji › how-to-create-an-array-containing-1-to-n-in-javascript-4fmh
How to Create an Array Containing 1 to N in JavaScript - DEV Community
December 24, 2020 - const numbers = Array.from( {length: 1000}, (item, index) => item = index + 1 ); console.log(numbers); // we get the same result! ... The Array.from() static method creates a new, shallow-copied Array instance from an array-like or iterable object.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-array-containing-1-to-n
Create an Array containing 1 to N numbers in JavaScript | bobbyhadz
Copied!function generateArray(size, ... from start to N. If you want to create an array from 0 to N, you can also use the Array() constructor....
🌐
Latenode
community.latenode.com › other questions › javascript
How can I generate an array with values 1 to N? - JavaScript - Latenode Official Community
September 30, 2024 - Array(N).keys(): This creates an array with N slots, each containing its index (0 to N-1). Spread Operator [...arr]: Turns the array-like object returned by keys() into a real array. map(n => n + 1): Adjusts each element, shifting the range ...
🌐
xjavascript
xjavascript.com › blog › how-to-create-an-array-containing-1-n
How to Create a JavaScript Array from 1 to N Without a Loop (Runtime N) — xjavascript.com
Generating an array from 1 to N without explicit loops is easy with JavaScript’s built-in methods. Array.from() is the most direct and readable, while Array.keys() + spread and fill() + map() offer alternatives for different coding styles.
Find elsewhere
🌐
W3docs
w3docs.com › home › code snippets › javascript › how to create an array containing 1…n
How to Create an Array Containing 1…N | W3docs
The <kbd class="highlighted">Array()</kbd> works like this: ... function Array() { if (arguments.length == 1 && 'number' === typeof arguments[0] && arguments[0] >= 0 && arguments && arguments[0] < 1) { return […]; // array of length arguments[0] ...
🌐
LinkedIn
linkedin.com › pulse › javascript-create-array-numbers-from-1-n-ketan-raval-zse6f
In JavaScript, Create an Array of Numbers from 1 to n
February 19, 2024 - We then use a for loop to iterate ... the console. Another approach to create an array of numbers from 1 to n is by using the Array.from() method....
🌐
Jstips
jstips.co › en › javascript › create-range-0 › .n-easily-using-one-line
Create array sequence `[0, 1, ..., N-1]` in one line
February 2, 2016 - Result: [0, 1, ..., N-1]. For a more thorough explanation, go here. It uses Array.from https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from
🌐
Devgex
devgex.com › en › article › 00000280
Comprehensive Guide to Creating 1 to N Arrays in JavaScript: Methods and Performance Analysis - DevGex
October 18, 2025 - Keywords: JavaScript | Array Creation | ES6 Syntax | Performance Optimization | Browser Compatibility · Abstract: This technical paper provides an in-depth exploration of various methods for creating arrays containing numbers from 1 to N in JavaScript.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-array-n-elements-same-value
Create an Array with N elements, same Value in JavaScript | bobbyhadz
Using the Array.from method is a little more explicit and easier to read than instantiating the Array constructor. When creating an array of N non-primitive elements with the same value, use the Array.from() method.
🌐
ACM@UIUC TIL
til.acm.illinois.edu › javascript › create-an-array-containing-1-to-n
Create An Array Containing 1 To N | ACM@UIUC TIL - Illinois
April 29, 2025 - > var N = 10; => undefined > Array.apply(null, {length: N}).map(Number.call, n => { return Number(n) + 1 }); => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
🌐
Milddev
milddev.com › javascript-create-array-with-n-elements
Create JS Array with n Elements - MildDev
June 24, 2025 - Array.from() is a versatile way to create arrays from iterable or array-like values—and it can generate n elements on its own: ... Here, the first argument is an object with a length property.
🌐
Jason Watmore's Blog
jasonwatmore.com › post › 2021 › 10 › 02 › vanilla-js-create-an-array-with-a-range-of-numbers-in-a-javascript
Vanilla JS - Create an array with a range of numbers in a JavaScript | Jason Watmore's Blog
October 2, 2021 - the Array() function creates a new javascript array with a specified number of empty slots (e.g. Array(10) creates the array [empty x 10]). the Array.keys() method returns a new iterator object with the key/index of each slot in an array (e.g. 0, 1, 2, ..., N). the ES6 Spread Operator (...) ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-range-create-an-array-of-numbers-with-the-from-method
JavaScript Range – How to Create an Array of Numbers with .from() in JS ES6
November 7, 2024 - In this article, you will learn what the .from() static method is all about, how it works, and how you can create a range of numbers in JavaScript. In case you are in a rush, here is a method to help you get the range: const arrayRange = (start, ...
🌐
npm Cheatsheet
lwebapp.com › en › post › fill-generates-array
JavaScript Implements the Number M to Form an Array of N Elements
August 24, 2023 - function generateArray(n, m) { if (n <= 0) { return []; } const array = []; for (let i = 0; i < n; i++) { array.push(m); } return array; } // Usage example const n = 5; //The number of elements in the array const m = 10; // element value in ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › Array
Array() constructor - JavaScript - MDN Web Docs
1 week ago - Note: Array() can be called with or without new. Both create a new Array instance. ... A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number (see the arrayLength parameter below).
🌐
Medium
medium.com › @ssubin615 › create-an-array-from-0-to-1000-with-style-in-javascript-js-38b8a8c1d9ec
Create an Array from 0 to 1000 with Style in Javascript (JS) | by Subin_S | Medium
October 16, 2023 - For instance, you can use (_, index) => 2 * index + 1 to make an array of odd numbers. Similarly, you can use (_, index) => 2 * index to generate even integers. You can create any pattern you want, with a little imagination.