• Use Array#push instead of direct assignment to add items to an array.

    1. const someStack = [];
    2. // bad
    3. someStack[someStack.length] = 'abracadabra';
    4. // good
    5. someStack.push('abracadabra');


  • 4.4 To convert an iterable object to an array, use spreads ... instead of .

    1. const foo = document.querySelectorAll('.foo');
    2. // good
    3. const nodes = Array.from(foo);
    4. // best
    5. const nodes = [...foo];

  • Use Array.from for converting an array-like object to an array.

  • Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following 8.2. eslint:

    1. // bad
    2. const arr = [
    3. [0, 1], [2, 3], [4, 5],
    4. ];
    5. const objectInArray = [{
    6. id: 1,
    7. }, {
    8. id: 2,
    9. }];
    10. 1, 2,
    11. ];
    12. // good
    13. const arr = [[0, 1], [2, 3], [4, 5]];
    14. const objectInArray = [
    15. {
    16. id: 1,
    17. },
    18. {
    19. id: 2,
    20. },
    21. ];
    22. const numberInArray = [
    23. 1,
    24. 2,