TreeviewCopyright © aleen42 all right reserved, powered by aleen42

Arrays Back

1. Use []

/**
 * bad
 */
const items = new Array();

/**
 * good
 */
const items = [];

2. Use push()

  • To use Array#push instead of direct assignment to add items to an array.
const arr = [];

/**
 * bad
 */
arr[arr.length] = 'aleen';

/**
 * good
 */
arr.push('aleen');

3. Use spread operator ...

  • To use array spreads to copy arrays.
/** 
 * bad
 */
const len = items.length;
const itemsCopy = [];
let i;

for (i = 0; i < len; i++) {
    itemsCopy[i] = items[i]; 
}

/**
 * good
 */
const itemsCopy = [...items];

4. Use from()

  • To convert an array-like object to an array, use Array#from.
const foo = document.querySelectorAll('.foo');
const arr = Array.from(foo);