0

What is the Difference between declaring an array with elements and declaring it and iterating the elements into it in Javascript?

r = [req.body.email,
     req.body.password];

vs

r=[];
r.push(req.body.email, req.body.password);
1
  • What kind of difference you're asking about? The result is the same. The code is different (two statements instead of one) Commented Jan 14, 2014 at 21:27

1 Answer 1

4

Biggest difference is code readability. They are functionally equivalent.

Depending on the javascript engine, one might perform faster than the other, but the difference is negligible.

You can measure the exact performance on jsperf, something like this: http://jsperf.com/assignment-vs-push/7

Run the test in different browsers to see different results, but looks to me like the direct assignment is faster.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this true of asynchronous javascript?
@Kinnard Hockenhull: how is it relevant? It's true for JS running on a black laptops

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.