3

Came across this code, and it had me scratching my head, and wondered is their benefits to grabbing the length to set the index.

var thisarray = new Array();

    function addtoArray(int){
        thisarray[thisarray.length] = int;
    }

over array.push

 function addtoArray(int){
        thisarray.push(int)
    }

Also, is there an equivalent to this php

thisarray[]
madphp
  • 1,716
  • 5
  • 31
  • 72
  • Yes, `thisarray[thisarray.length] = something` is equivalent to PHP's `thisarray[] = something`. And `push()` is equivalent to `array_push()`. – Frédéric Hamidi Jul 09 '13 at 15:39
  • This could be interesting: [Why is array.push sometimes faster than array\[n\] = value?](http://stackoverflow.com/questions/614126/why-is-array-push-sometimes-faster-than-arrayn-value). – insertusernamehere Jul 09 '13 at 15:43
  • 1
    Javascript's push is *not* "equivalent" to PHP's array_push. Javascript's push is an object/instance method; it is called directly on the array using dot notation. With PHP's array_push, you must pass the array as an argument. – Dexygen Jul 09 '13 at 15:44
  • possible duplicate of [Is there a reason JavaScript developers don't use Array.push()?](http://stackoverflow.com/questions/15649899/is-there-a-reason-javascript-developers-dont-use-array-push) – Bergi Jul 09 '13 at 15:47
  • 2
    In my experience not using push is an indication that the developer has been using Javascript a very long time, since *before* push was introduced/fully-supported -- I can remember when it wasn't an option, back around 1998/99. – Dexygen Jul 09 '13 at 15:59

2 Answers2

4

In the example you have posted, the two uses are the same. Both append a new element to the end of the array.

However, the push() method can take multiple arguments and so you can append multiple elements to the end of the array in one statement. push() then returns the new length of the array. push() is also shorter and arguably easier to read.

Another thing to consider is that if thisarray has been incorrectly defined (ie. it is not an Array object) then thisarray[thisarray.length] = int; is likely to fail silently since .length will simply be undefined. Whereas thisarray.push(int) will fail with a trappable TypeError exception.

Regarding PHP's thisarray[] (square bracket) syntax. There is nothing quite the same in JavaScript, as far as syntax goes. However, thisarray[thisarray.length] = int; performs the same action.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
2

thisarray[thisarraylength] = int and thisarray.push(int) are identical. Arguably, the only advantage the latter has over the former is readability.

You might also find answers to this question useful: Why is array.push sometimes faster than array[n] = value?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    To see how they are identical you can check out this snippet from the v8 source, https://gist.github.com/5958737. Also see: http://people.mozilla.org/~jorendorff/es5.html#sec-15.4.4.7 – travis Jul 09 '13 at 16:16