Should You Truly Never Use var? – John Wolfe – Medium

Should You Truly Never Use Var?  #ecmascript6 #ydkjs #javascript #variables #react #reactjs

  • No error is thrown if you declare the same variable twice using var (conversely, both let and const will throw an error if a variable is declared twice)2.
  • So using var in this context would require a situation in which a variable declared inside a function would need to be used in the global scope.
  • I conducted a quick search on the Internet revealed that Jslint.com and Jshint.com, and did not raise an issues with the use of var as a variable declaration.
  • Kyle Simpson, author of You Don’t Know JS, and all-around Javascript guru, in this article, has argued for a possible case for var.
  • Simpson argues that:“There are going to be places in real world code where some variables are going to be properly scoped to the entire function, and for those variables, var is a better signal”He provides this code block to illustrate his point:Essentially, Simpson is arguing that while changing var with…

Some controversy in the Javascript community has emerged over this classic variable declaration. So, are the E6 purists correct and we should forever delegate var to the dustbin? This blog post will…

Should You Truly Never Use var?

Some controversy in the Javascript community has emerged over this classic variable declaration. So, are the E6 purists correct and we should forever delegate var to the dustbin? This blog post will get to the bottom of the debate.

First, the arguments against var (the first two adopted from Learn Verified’s new and excellent V3 curriculum).

1. No error is thrown if you declare the same variable twice using var (conversely, both let and const will throw an error if a variable is declared twice)

2. Variables declared with var are not block scoped (although they are function scoped), while with let and const they are. This is important because what’s the point of block scoping if you’re not going to use it. So using var in this context would require a situation in which a variable declared inside a function would need to be used in the global scope. I’m not able to think of any situations where that would be absolutely necessary, but perhaps they exist.

3. Another argument that has been made against the use of var is that JS Linters are now pointing out their use as bad practice. I conducted a quick search on the Internet revealed that Jslint.com and Jshint.com, and did not raise an issues with the use of var as a variable declaration. However, ESLint explicitly has a no-var rule aimed at discouraging the use of var.

Should You Truly Never Use var? – John Wolfe – Medium