Using the push method or .length when adding to array?

后端 未结 6 1820
慢半拍i
慢半拍i 2020-12-31 08:26

What are the downsides to doing:

var myArray = [];
myArray[myArray.length] = val1;
myArray[myArray.length] = val2;

instead of:



        
6条回答
  •  灰色年华
    2020-12-31 08:58

    Since arrays in JavaScript do not have holes the functionality of those two methods is equal. And yes, using .push() is much cleaner (and shorter).

提交回复
热议问题