Search code examples
javascriptconstantsvarecmascript-6let

Is there any reason I wouldn't use "let"?


Now that we can write ES6 and deploy it to browsers (using Traceur or 6to5 to support legacy user agents), is there any reason why we wouldn't use let or const as our default keywords for variable declaration?

Is var dead? And if it is, can I configure my linting tools to reject it?


Solution

  • As es5 only has function scope for variables, presumably your transpiler is creating closures in order to mock the let keyword's functionality. That may have an effect on the resulting size of your code if you're declaring variables inside scopes that are not functions (e.g. loops, ifs etc). So that is one reason not to, currently. It may also make debugging slightly more confusing, although this could be mitigated using sourcemaps.

    There aren't really any other drawbacks. Variable hoisting is pretty confusing to people coming from other languages, and using let allows you to avoid this potential hiccup. So I would use it now if you can.