1

Doing Array.apply(null,[1]) gets me this [empty] but Array(null,[1,2]) gets me [1,2] the array with 'empty' has a length of one but index zero is undefined.

Array.apply(null,[1]).length 1 Array.apply(null,[1])[0] undefined Array.apply(null,[1]) [empty]

console.log(Array.apply([1]));
console.log(Array.apply([1]).length);

7
  • Also, Array.apply calls Function#apply since Array is a (constructor) function, and supplies [1] as the this value with 0 arguments when calling Array. Commented May 19, 2019 at 2:33
  • But why is the empty item there? I put in a vaild value and Array.apply([1,2]) seems to work just fine Commented May 19, 2019 at 2:35
  • @Li357 I understand what apply does it was just the context I used it in. also wouldn't the 1 be an argument? Commented May 19, 2019 at 2:40
  • 1
    Array.apply([1]).length evaluates to 0, not 1. Commented May 19, 2019 at 2:43
  • @TheIntellectual No, the first argument to apply is the context. Commented May 19, 2019 at 2:48

2 Answers 2

2

There is nothing like empty variable in Javascript, they are empty slots. You are calling Array([1,2]) instead Array.apply([1,2]). Both Array.apply([1]) and Array.apply([1,2]) will give you same result which will be [].

But when you call Array.apply(this,[1]) it will result in [empty] because internally it is equal to Array(1). And when you call Array.apply(this,[1,2]) it is equal to Array(1,2) which will be [1,2]. When only one number passed to Array constructor it returns an array of empty slots with its length property set to that number.

MDN Docs

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

4 Comments

I forgot the null for the "this" but Array.apply(null,[1]) gets me [empty]
FYI, empty is just a thing the browser outputs for a more friendly output. It usually means an array with length but no contents.
@Li357 but why is there no content?
@TheIntellectual See the MDN docs. It states that if there is only one argument it's treated as the array length with no content (empty slots)
1

The reason you are getting two different results is that in the first case of doing:

Array.apply(null,[4])

You are simply invoking the JavaScript Array constructor with a list of one argument. This single argument to the constructor is used to create an array of size n where n is the number you have in the list. It creates this list without anything in it.

For me in a node REPL, this is the result of the above:

> Array.apply(null, [4])
[ <4 empty items> ]

In the second case where you did:

Array.apply(null, [1, 2])

This is another constructor overload used to specify the contents of the array. So in this case, you are telling the array constructor to create an array containing the elements 1, 2.

> Array.apply(null, [1, 2])
[ 1, 2 ]

See the MDN documentation for details.

10 Comments

So the number is to tell it to make an array that has no values but has length of 1?
@TheIntellectual Yes. the number is used to tell the constructor how big to make the array; aka the length.
so this would get me an array with all values being 4? Array(4).map(item => 4)
@TheIntellectual try Array(4).fill(4). See this interesting article explaining why map didn't work
Array(4).fill(0).map(item => Math.floor(Math.random()*10)) works but Array(4).map(item => Math.floor(Math.random()*10)) doesn't
|

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.