Chomper Stomping jQuery/JavaScript/CSS 3/HTML 5, Java/PHP/Python/ActionScript, Git, Chrome/Firefox Extensions, Wordpress/Game/iPhone App Development and other random techie tidbits I've collected

10Feb/101

JavaScript Arrays – new Array() vs array literal

1306092411709_a923d

Do not every say new Array() when creating a new JavaScript Array(). There are differences between creating an array in Javascript using the array literal, or the fully qualified "object" name (Array() vs []), and the bottom line is that the "right" way to do it is "[]" (array literal) instead of "new Array()". Here is why:

Comments (1) Trackbacks (0)
  1. Nice post!
    I always thought the reason for using brackets instead of new was only commodity and minification advantages, so thanks for the info.
    By the way, there’s one more plus to using the array literal: it’s faster ( http://jsperf.com/new-array-vs-square-brackets/5 ). Even though the difference it’s not that great at this scale, it still counts for something


Leave a comment

No trackbacks yet.