var StronglyTypedArray=function(){
    this.values=[];
    this.push=function(value){
    if(value===0||parseInt(value)>0) this.values.push(value);
    else return;//throw exception
   };
   this.get=function(index){
      return this.values[index]
   }
}

EDITS: use this as follows

var numbers=new StronglyTypedArray();
numbers.push(0);
numbers.push(2);
numbers.push(4);
numbers.push(6);
numbers.push(8);
alert(numbers.get(3)); //alerts 6
Answer from Bellash on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Typed_arrays
JavaScript typed arrays - JavaScript | MDN
Int16Array | 32 | 0 | 2 | 0 | 4 | 0 | 6 | 0 | Int32Array | 32 | 2 | 4 | 6 | ArrayBuffer | 20 00 00 00 | 02 00 00 00 | 04 00 00 00 | 06 00 00 00 | You can do this with any view type, although if you set an integer and then read it as a floating-point number, you will probably get a strange result because the bits are interpreted differently.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
Both the original and new array refer to the same object. That is, if a referenced object is modified, the changes are visible to both the new and original arrays. Primitive types such as strings, numbers and booleans (not String, Number, and Boolean objects): their values are copied into the ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Data_structures
JavaScript data types and data structures - JavaScript | MDN
Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types: ... let foo = 42; // foo is now a number foo = "bar"; // foo is now a string foo = true; // foo is now a boolean
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-array-typeof
JavaScript Typeof for Data Types: Array, Boolean and More | Built In
The typeof operator in JavaScript is used to check the data type of a variable in code. It can determine if a variable's data type is an array, boolean or other. Written by Dr. Derek Austin ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
JavaScript has some lower-level functions that deal with the binary encoding of integer numbers, most notably bitwise operators and TypedArray objects. Bitwise operators always convert the operands to 32-bit integers.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray
TypedArray - JavaScript | MDN
It stores the number in binary like Uint8Array does, but when you store a number outside the range, it clamps the number to the range 0 to 255 by mathematical value, instead of truncating the most significant bits. All typed arrays except Int8Array, Uint8Array, and Uint8ClampedArray store each element using multiple bytes.
🌐
typescriptlang.org
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
JavaScript does not have a special runtime value for integers, so there’s no equivalent to int or float - everything is simply number ... The type names String, Number, and Boolean (starting with capital letters) are legal, but refer to some special built-in types that will very rarely appear in your code. Always use string, number, or boolean for types. To specify the type of an array ...
Top answer
1 of 16
3976

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-convert-a-number-into-array-in-javascript
JavaScript - Convert a Number into JS Array - GeeksforGeeks
Another approach to convert a number into an array in JavaScript is by using the split() method directly on the string representation of the number.
Published   July 23, 2025
🌐
Medium
medium.com › @julienetienne › different-types-of-arrays-in-javascript-and-when-to-use-them-77f7843b71de
Different Types of Arrays in JavaScript + When to Use Them | by Julien Etienne | Medium
February 8, 2024 - In Golang, this would be a Slice, Python has List and JavaScript has Array. There are many nuances regarding Arrays in JS: with holes, without, is an Object etc. This is my rule of thumb: If it’s not going to be beneficial don’t obsess about it · With that said, I want to touch on just the most important aspects of Arrays in JS. If you don’t already know, you can type-check arrays using Array.isArray() which I tend to destructure using const { isArray } = Array.
🌐
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 - Initializes an empty array newArr. Uses a for loop to iterate from 1 to N, adding each number to newArr with push().
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Arrays
That’s essentially the same as obj[key], where arr is the object, while numbers are used as keys. They extend objects providing special methods to work with ordered collections of data and also the length property.
🌐
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 - The .from() method is a static method of the Array object in JavaScript ES6. It creates a new, shallow-copied Array instance from an array-like or iterable object like map and set. This method returns an array from any object with a length property. You can use it to create an Array of numbers within a specified range.
🌐
W3Schools
w3schools.com › js › js_typed_arrays.asp
JavaScript Typed Arrays
Typed arrays store elements of Fixed Types like 8-bit integers or 32-bit numbers. ... Typed Arrays were designed to provide an efficient way to handle binary data, unlike traditional JavaScript arrays which can hold elements of mixed data types.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-data-types
JavaScript Data Types - GeeksforGeeks
They are fundamental as nearly everything in JavaScript is an object. ... An Array is a special kind of object used to store an ordered collection of values, which can be of any data type.
Published   April 24, 2019