3

Actually, I am working on someone others code.

While resolving a bug I found such types of things.

Note: API is giving productNumber in number type.

Sometimes passing only one productNumber in new Array(). then a new bug occurs.

And when entering productNumber more than one then working fine.

Why arr1[0] is giving undefined.

var productNumber1 = 1001;
var arr1 = new Array(productNumber1);
console.log("arr1",arr1[0]);

var productNumber2 = '1001';
var arr2 = new Array(productNumber2);
console.log("arr2",arr2[0]);

Could you please someone explain this behavior.

3 Answers 3

4

In case1 the argument you passing getting treated as a length of the array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

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). Note that this special case only applies to JavaScript arrays created with the Array constructor, not array literals created with the bracket syntax.

arrayLength

If the only argument passed to the Array constructor is an integer between 0 and 2^32-1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values). If the argument is any other number, a RangeError exception is thrown.

So actually in case1, you are creating an array with length 1001.

However in case 2, that is getting treated as an element since it's not a number.

Sign up to request clarification or add additional context in comments.

4 Comments

Just a small correction: one doesn't pass a parameter, but an argument instead. Parameters never change, arguments do.
@GerardoFurtado Ahh avoided that confusion.
So "any other number, a RangeError exception is thrown" means (productNumber<0 || productNumber>2^32-1) will produce an exception.
@AnkitPandey Cool. Glad that it helped.
2

This is described in the documentation for the Array constructor:

If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with its length property set to that number

So if you pass a number like 1001 you'll set the length, but if you pass a string like '1001', you'll set the first value, and if you pass multiple arguments you will set multiple values.


Just don't use the Array constructor. Use the square bracket literal notation instead. That avoids the problem.

var productNumber1 = 1001;
var arr1 = [productNumber1];
console.log("arr1",arr1[0]);

var productNumber2 = '1001';
var arr2 = [productNumber2];
console.log("arr2",arr2[0]);

Comments

1

You may also use the Array.of() tool for this job.

var p1 = 1001,
    p2 = "1001",
    a1 = Array.of(p1),
    a2 = Array.of(p2);

console.log(a1);
console.log(a2);

1 Comment

Nice solution of my question. Array.of() is part of es6.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.