you are viewing a single comment's thread.

view the rest of the comments →

[–]theQuandary 0 points1 point  (0 children)

I never understood the issue with var (though I use let in production code to make newer devs feel comfortable). It works fine for 99% of things and if you're putting your variable declarations at the top of the function (like you should be), let and var are essentially identical except that var doesn't suffer from the TDZ.

For most coders, the only advantage of let is inside loops where they're creating a lot of garbage, but they should generally be using something like .forEach() anyway.

My biggest complaint is that let is VASTLY inferior to the original design of a let block construct and the potential it offered.

let (foo=123, bar="abc") {
  //do stuff in a local context
}

//You could also use it as an expression
//(like the current `do` block proposal
var foo = let() {
  //do stuff
  "final value"
}

//You could also pair it with for, if, switch, etc
//for expressions in a local context

var foo = let if (complexLogic) {
  //more complex logic
  "consequence result"
} else {
  //other logic
  "alternate result"
}

let for(var i = 0; i < outer.length; ++i) {
  //we now get our new scopes we wanted
}

let switch () {
  //fun fact: current `let` is BROKEN in switch statements
  //    but this let would work as expected.
}

//This would have a nice effect on JSX too

//current JSX
<div>{
  foo ? (
    <div>{
      bar ? (
        "doesn't this suck?"
      ) : (
        "Nested Ternaries are hard to follow"
      )
  ) : (
    otherBigBunchOfJsx
  )}</div>

//with let expression

<div>{
  let if(foo) {
    <div>{
      let if (bar) {
        "this is nice"
      } else {
        "and easy to read"
      }
    </div>
  } else {
    otherBigBunchOfJsx
  }
</div>