Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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[]
share|improve this question
    
Yes, thisarray[thisarray.length] = something is equivalent to PHP's thisarray[] = something. And push() is equivalent to array_push(). –  Frédéric Hamidi Jul 9 '13 at 15:39
    
This could be interesting: Why is array.push sometimes faster than array[n] = value?. –  insertusernamehere Jul 9 '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. –  George Jempty Jul 9 '13 at 15:44
    
possible duplicate of Is there a reason JavaScript developers don't use Array.push()? –  Bergi Jul 9 '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. –  George Jempty Jul 9 '13 at 15:59

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer

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?

share|improve this answer
1  
To see how they are identical you can check out this snippet from the v8 source, gist.github.com/5958737. Also see: people.mozilla.org/~jorendorff/es5.html#sec-15.4.4.7 –  travis Jul 9 '13 at 16:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.