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

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

    Mostly, people use var a = [] because Douglas Crockford says so.

    His reasons include the non-intuitive and inconsistent behaviour of new Array():

    var a = new Array(5);     // an array pre-sized to 5 elements long
    var b = new Array(5, 10); // an array with two elements in it
    

    Note that there's no way with new Array() to create an array with just one pre-specified number element in it!

    Using [] is actually more efficient, and safer too! It's possible to overwrite the Array constructor and make it do odd things, but you can't overwrite the behaviour of [].

    Personally, I always use the [] syntax, and similarly always use {} syntax in place of new Object().

    0 讨论(0)
  • 2020-11-28 02:38

    for maintainability, use []

    The array literal is more predictable, as most developers use it. Most array usage out there will be using the literal, and there is value in having your code match up with what other developers use.

    for empty arrays, use []

    var ns = [];
    var names = [ 'john', 'brian' ];
    

    As shown here, using the literal for empty and a couple of known elements is fatster than the Array constructor.

    for an array of known size, use new Array(size)

    If the size is known, then using the Array constructor significantly improves performance. However it also means you have to deal with an array which already has 'undefined' filling all it's values.

    As shown here

    // fill an array with 100 numbers
    var ns = new Array( 100 );
    for ( var i = 0; i < 100; i++ ) {
        ns[i] = i;
    }
    

    This also works for very small arrays too.

    0 讨论(0)
  • 2020-11-28 02:41

    No, there is actually no reason to use one notation over the other one for empty Arrays.

    However, most browsers show a slightly better performance using x = []; than calling the Constructor.

    If you need to create an Array with a specific size, you kind of need to use x = new Array(10); for instance, which would create an Array with 10 undefined slots.

    0 讨论(0)
  • 2020-11-28 02:43
    var array = [ 1, 2, 3, 4];
    

    is sugar

    var array = new Array(1, 2, 3, 4);
    

    is salt

    0 讨论(0)
  • 2020-11-28 02:44

    Both are correct only. But most of the people use var a = []

    Three Ways to Declare an Array in JavaScript.

    method 1: We can explicitly declare an array with the JavaScript "new" keyword to instantiate the array in memory (i.e. create it, and make it available).

    // Declare an array (using the array constructor)
    var arlene1 = new Array();
    var arlene2 = new Array("First element", "Second", "Last");
    

    method 2: we use an alternate method to declaring arrays.

    // Declare an array (using literal notation)
    var arlene1 = [];
    var arlene2 = ["First element", "Second", "Last"];
    

    method 3: JavaScript also lets you create arrays indirectly, by calling specific methods.

    // Create an array from a method's return value
    var carter = "I-learn-JavaScript";
    var arlene3 = carter.split("-");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题