References

  • 2.1 Use const for all of your references; avoid using var. eslint: prefer-const, no-const-assign

    Why? This ensures that you can’t reassign your references, which can lead to bugs and difficult to comprehend code.

    1. // bad
    2. var a = 1;
    3. var b = 2;
    4. // good
    5. const a = 1;
    6. const b = 2;

  • 2.2 If you must reassign references, use let instead of var. eslint: no-var

    Why? let is block-scoped rather than function-scoped like var.

    1. // bad
    2. var count = 1;
    3. if (true) {
    4. count += 1;
    5. }
    6. // good, use the let.
    7. let count = 1;
    8. if (true) {
    9. count += 1;
    10. }

  • 2.3 Note that both let and const are block-scoped.

    1. // const and let only exist in the blocks they are defined in.
    2. {
    3. let a = 1;
    4. const b = 1;
    5. }
    6. console.log(a); // ReferenceError
    7. console.log(b); // ReferenceError