• Use one const or let declaration per variable. eslint: one-var

    1. // bad
    2. const items = getItems(),
    3. goSportsTeam = true,
    4. dragonball = 'z';
    5. // bad
    6. // (compare to above, and try to spot the mistake)
    7. const items = getItems(),
    8. goSportsTeam = true;
    9. dragonball = 'z';
    10. // good
    11. const items = getItems();
    12. const goSportsTeam = true;
    13. const dragonball = 'z';

  • Group all your consts and then group all your lets.

  • 13.4 Assign variables where you need them, but place them in a reasonable place.

    1. // bad - unnecessary function call
    2. function checkName(hasName) {
    3. const name = getName();
    4. if (hasName === 'test') {
    5. return false;
    6. }
    7. if (name === 'test') {
    8. this.setName('');
    9. return false;
    10. }
    11. return name;
    12. }
    13. // good
    14. function checkName(hasName) {
    15. return false;
    16. }
    17. const name = getName();
    18. if (name === 'test') {
    19. this.setName('');
    20. return false;
    21. }
    22. return name;
    23. }

  • Don’t chain variable assignments. eslint: no-multi-assign

    Why? Chaining variable assignments creates implicit global variables.

  • Avoid using unary increments and decrements (++, --). eslint no-plusplus

    1. // bad
    2. const array = [1, 2, 3];
    3. let num = 1;
    4. num++;
    5. --num;
    6. let sum = 0;
    7. let truthyCount = 0;
    8. for (let i = 0; i < array.length; i++) {
    9. let value = array[i];
    10. sum += value;
    11. if (value) {
    12. truthyCount++;
    13. }
    14. }
    15. // good
    16. const array = [1, 2, 3];
    17. num += 1;
    18. const sum = array.reduce((a, b) => a + b, 0);
    19. const truthyCount = array.filter(Boolean).length;

  • 13.7 Avoid linebreaks before or after = in an assignment. If your assignment violates , surround the value in parens. eslint operator-linebreak.

    Why? Linebreaks surrounding = can obfuscate the value of an assignment.

    1. // bad
    2. var some_unused_var = 42;
    3. // Write-only variables are not considered as used.
    4. var y = 10;
    5. y = 5;
    6. // A read for a modification of itself is not considered as used.
    7. var z = 0;
    8. z = z + 1;
    9. // Unused function arguments.
    10. function getX(x, y) {
    11. return x;
    12. }
    13. // good
    14. function getXPlusY(x, y) {
    15. return x + y;
    16. }
    17. var x = 1;
    18. var y = a + 2;
    19. alert(getXPlusY(x, y));
    20. // 'type' is ignored even if unused because it has a rest property sibling.
    21. // This is a form of extracting an object that omits the specified keys.
    22. var { type, ...coords } = data;