Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let, const issues #136

Open
calvinmetcalf opened this issue Jan 24, 2014 · 0 comments
Open

let, const issues #136

calvinmetcalf opened this issue Jan 24, 2014 · 0 comments

Comments

@calvinmetcalf
Copy link
Contributor

projects are going to need to be consistent in let/const usage, some options

  1. only use var (no let/const)
  2. use var unless a variable needs to be block scoped, then use let.
  3. use let for everything
  4. use const where possible, otherwise use let
  5. use const for values (strings, numbers, booleans, null, undefined) that are constant and other immutable things (frozen objects), otherwise use let.

recommendations should probably be something along the lines of pick a strategy and treat it like gospel. The difference between 4 and 5 is that for objects it's the reference to the object that is constant, not the values in the object. Since most object references are more often then not never redefined that means that in version 4 const ends up being used almost exclusively.

If we only wanted one let/const option then 5 would be the best.

The other thing about let and const is that they always have the exact scope they appear to have, meaning that declaring variables at the top of scope has less of an benefit but continues to have the downside of increasing the change a forgetting to declare a variable, e.g.

function () {
  let a,b,c;
  //stuff
  while(true){
    let d;
    //lots of stuff, not with d.
  }
  // lots of stuff
  d = 9;
  //whoops
}

meaning an argument could be made that for let and const it is a better practice to declare them when they are first assigned a value or as close to that as possible, e.g.

function () {
   // stuff
  let a; //non local variable.
  while(true){
    //stuff
    let b = something;
    a = somethingElse
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant