Here is the examples
Array(1,2,3)
> [1,2,3]
new Array(1,2,3)
> [1,2,3]
Array(3)
> [undefined, undefined, undefined]
new Array(3)
> [undefined, undefined, undefined]
I saw some comments about "never use Array with new". But I can't understand as I found Array and new Array seems to behave the same in Javascript.. Why do they behave the same? And why should one usage be preferred over the other?
I know [] literal syntax should usually be used instead of Array, I was just wondering what Array is..
Is it a constructor function? If it is a constructor function, why could it be used without new?
new Array()norArray()should be used. Use the literal syntax instead, i.e.[1, 2, 3]. Btw there is nothing in JavaScript from stopping you to call a constructor function withoutnew.[]version :)Array(1,2,3)ornew Array(1,2,3). Always use[1,2,3].[]is usually the best choice.. Just wondering whatArrayactually is..new Array()behaves in unexpected ways. E.g.new Array(8)initializes an 8 element array, whereasnew Array(8, 9)initializes an array with the elements 8 and 9.[8]on the other hand gives you an array containing the element 1. Also[]is shorter.