A matter of style

Like the previous chapters, we will spend some time discussing the style considerations while creating arrays.

  • Use the literal syntax for array creation:
    // bad
    const items = new Array();
    // good
    const items = [];
  • Use Array#push instead of a direct assignment to add items to an array:
    const stack = [];
    // bad
    stack[stack.length] = 'pushme';
    // good
    stack.push('pushme');

Get Mastering JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.