What are the best practices to follow when declaring an array in Javascript?

后端 未结 9 1131
闹比i
闹比i 2020-11-28 02:11

When I need to declare a new array I use this notation

var arr = new Array();

But when testing online, for example on jsbin, a warning sign

9条回答
  •  星月不相逢
    2020-11-28 02:54

    It's because new Array() is ambiguous. These are the correct constructors:

    // Using brackets
    [element0, element1, ..., elementN]
    
    // Using new AND a list of elements
    new Array(element0, element1, ..., elementN)
    
    // Using new AND an integer specifying the array length
    new Array(arrayLength)
    

提交回复
热议问题