Is there a reason JavaScript developers don't use Array.push()?

前端 未结 6 1167
庸人自扰
庸人自扰 2020-12-01 05:29

I commonly see developers use an expression like the following in JavaScript:

arr = []
arr[arr.length] = \"Something\"
arr[arr.length] = \"Another thing\"
         


        
6条回答
  •  攒了一身酷
    2020-12-01 06:01

    Array.prototype.push method returns the updated array's length while the direct declaration returns the value that is being assigned. That's the only difference I see, but I usually read some best-practices recommendations about the push method being a better way to assign new values to an array.

提交回复
热议问题