Why doesn't `new Array(new Number(3))` produce an array of length 3?

When I write new Array(new Number(3)) in the console, it produces an array of length 1 with that item being a Number object with valueOf=3. Why does the array constructor treat new Number(3) different to the primitive version of 3?

  1. Can someone link me to a standards doc where this Array behaviour is specified?
  2. Is this something that I need to watch out for in general, or is it specific to the Array constructor?

Thanks!

Because in new Array(new Number(3)), the parameter new Number(3) returns an object and not the number.

Where as new Array(Number(3)) will work as Number(3) returns 3, the number.

Thanks to @Andreas' comment, here's the specification: ECMAScript 22.1.1.2 Array (len)

Step 7: "If Type(len) is not Number, then

  1. Let defineStatus be CreateDataProperty(array, "0", len).
  2. Assert: defineStatus is true.
  3. Let intLen be 1.

Why doesn't loyalty seem to pay with Nationwide savings?, Internet banking doesn't work for one reader during the coronavirus crisis. Why doesn’t Trump? A precinct deputy in St. Petersburg, Fla., wears gloves to assist voters dropping off ballots during Florida's primary election on March 17. (Zack Wittman/For The Washington Post)

From the MDN

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

Instance of new Number(3) is not an integer(number).

Number.isInteger(3) // -> true

Number.isInteger(Number(3)) // -> true

Number.isInteger(new Number(3)) // -> false

Why Trump doesn't want to wear a mask - CNN, It's no surprise that President Donald Trump says he won't follow CDC recommendations to wear a mask to stop the spread of coronavirus,  If God Can Stop a Pandemic Why Doesn't He? Let us consider that God may choose not to stop this pandemic, simply because He cares more about the disease in our hearts than disease in our bodies

Why doesn't Sen. McSally LOVE communists? After all, Donald , Why doesn't Sen. Martha McSally LOVE communists? After all, Donald Trump does. EJ Montini, Arizona Republic Published 8:53 a.m. MT May  Why doesn’t He show Himself to each generation of humanity so that everyone on Earth can see and hear Him and know beyond a shadow of a doubt that He is real? After all, according to the Bible, the Lord appeared “to Abraham, to Isaac, and to Jacob, as God Almighty” (Exodus 6:3), and He “spoke to Moses face to face, as a man speaks to

Array can take either n parameters representing its elements, or a number with the array length (syntax).

new Array(element0, element1[, ...[, elementN]])

elementN 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.

new Array(arrayLength)

arrayLength 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 (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.

If you console log the following values, you will see that new Number(3); is not an integer.

typeof 3
"number"

typeof Number(3)
"number"

typeof new Number(3)
"object"

As new Number(3) is an object it thinks it's elementN scenario where you are passing the first element of the array.

If you use new Array(Number(3)); however, this would work because Number(3) is of type number.

You can also refer to the definition of new Number() (syntax):

value

The numeric value of the object being created.

It is clear that it is returning an object from this explanation. When you are doing Number(x), what happens is a type casting:

In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.

This explains why Number(3) would work as it would be casting 3 (or "3") as a number.

console.log(typeof 3);  // number
console.log(typeof Number(3));  // number
console.log(typeof Number("3"));  // number
console.log(typeof new Number(3)); // object


console.log(new Array(3)); // [,,]
console.log(new Array(Number(3))); // [,,]
console.log(new Array(Number("3"))); // [,,]
console.log(new Array(new Number(3))); // [{}]

Why Doesn't Microfinance Work?: The Destructive Rise of Local , Why Doesn't Microfinance Work?: The Destructive Rise of Local Neoliberalism [​Bateman, Milford] on Amazon.com. *FREE* shipping on qualifying offers. Try the disc in another computer's drive. This will determine only the integrity of the disc, not whether your drive will read it. If your disc still won't work in your drive, but will in another, then continue to the next step. However, if the disc doesn't work in any computer, it's probably defective and should be replaced.

Because new Number() is a constructor that creates an object of type Number. If you initialise an array using it's constructor new Array() with an object it becomes a member of that array. If you initialise an array using it's constructor new Array() with a numeric value it will create an empty array of that size.

Just type into your console:

var n = new Number(3);

You get an object of type Number with a primitive value of 3, this is different from

var n = 3;

Why doesn't the Leaning Tower of Pisa fall over?, Published: Alesina, Alberto, Edward Glaeser and Bruce Sacerdote. “Why Doesn't the United States Have a European-Style Welfare State?” Brookings Paper on  Why doesn't God perform a spectacular miracle and prove himself? Q: "Why are there no clearly visible miracles today that shout -- without a shadow of a doubt -- to everyone in the world: 'There is a God!'?"

A president should want all Americans to vote. Why doesn't Trump , “Why Doesn't She Just Leave?”: A Descriptive Study of Victim Reported Impediments to Her Safety. Michael A. Anderson ,; Paulette Marie Gillig ,; Marilyn Sitaker  So why doesn’t your cat purr? Like I always say, cats are individuals. They communicate in many different ways — vocalizations, facial expressions, body language. It may simply be that your cat has learned other ways to communicate and doesn’t have a need to purr. Or his purr may be so soft that you just don’t notice it.

Why Doesn't the US Have a European-Style Welfare System?, Why doesn't IRS payment tracker work? The latest stimulus check answers. By Sarah Skidmore Sell. Associated Press  Even if you choose to stay signed in when you log in to Yahoo, Yahoo still might prompt you to log in each time you check your Yahoo Mail.If this is the case, then the browser isn't saving login cookies, which are bits of data that let Yahoo know you're a return visitor.

“Why Doesn't She Just Leave?”: A Descriptive Study of Victim , Jesus performed countless miracles, yet the vast majority of people did not believe in Him. If God performed miracles today as He did in the past, the result would be the same. People would be amazed and would believe in God for a short time. That faith would be shallow and would disappear the moment something unexpected or frightening occurred

Comments
  • ECMAScript 22.1.1.2 Array (len) -> Step 7: "If Type(len) is not Number, then ..." with typeof new Number(3) === "object"
  • This was also useful to understand that new Number (or Boolean) isn't really useful, since it wraps the plain scalar into an object: stackoverflow.com/q/4719320/500207
  • Thanks! If you can edit in @Andreas' comment with the ECMAScript link, then I will accept this answer :)
  • It might be good to illustrate this by showing the results of calling Number.isInteger(): Namely, Number.isInteger(new Number(3)) === false but Number.isInteger(3) === true.
  • @HereticMonkey, thanks this will complete an answer.