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

后端 未结 9 1110
闹比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:45

    There is a limit the constructor can take as arguments.

    On most systems I encoutered the limit is 2^16 (2 bytes):

    var myFreshArr = new Array(0,1,2,3 ..... 65536);
    

    That will cause an error (too many arguments....)

    When using the literal [] you don't have such problems.

    In case you don't care about such big arrays, I think you can use whatever you prefer.

提交回复
热议问题