DEV Community

Discussion on: Understanding Javascript Array Series II - Alternate ways of Creating an Array.

Collapse
 
aphatheology profile image
Abdulkareem Mustopha

A newbie is confused...
Why is array().fill and array.of returning undefined for numbers?

Collapse
 
nedyudombat profile image
Nedy Udombat

Hi @aphatheology Array.fill() takes three arguments.

The first argument is the value you want to fill the array with, it can range from a string to an number to to even another array.

The second argument is the index of the array in which you want to start filling the array from. If you do this Array(5).fill(3, 2). This means you want to fill the array from index 2 till the end with the value if 3. This will return [undefined, undefined, 3, 3, 3]

The third argument is the index position of the array in which you want to stop filling the array. Array(5).fill(3, 2, 4) will give you this [undefined, undefined, 3, 3, undefined].

Array.of() doesn't return undefined for numbers.