3

WHY NOT TO BAN 'VAR'? My question is not about the difference between 'var' and 'let'. All such answers are advocating the advantages of using 'let'. My question is: why not to tell frankly "do not use 'var' anymore"? Is there a reason for not being so direct?

'var' is still in use on many serious tutorial sites: Mozilla MDN, w3schools ... I am wondering if there is an hidden reason that I am missing.

There is one answer below: legacy (old browsers not supporting ES6) Is that the only reason? Any performance reason? Or some fancy use of hoisting?

[ Here was the rest of my original post...

var arr = [];    // -> const arr = [];
var obj = {};    // -> const obj = {};
var temp;        // -> let temp;
var f = function(){};    // -> const f = function(){};

Doing so, I think that the only way a variable may behave like a var variable (hoisting etc.), is an -unfortunately- undeclared variable: x = sthg; in some function (becoming: var x = sthg; at global scope). If I missed something, it would highly help me. ]

allez l'OM
  • 547
  • 4
  • 13
  • 1
    Only if the target browser doesn't support ES6 – sumeet kumar Jan 05 '18 at 16:24
  • 1
    `var` does variable hoisting. There might be a use case where someone wants that to happen. But I agree that `let` and `const` should be used instead. – byxor Jan 05 '18 at 16:24
  • Also you have to be cautious about scopes - https://stackoverflow.com/questions/2485423/is-using-var-to-declare-variables-optional – Thiyagu Jan 05 '18 at 16:27
  • 1
    Sorry, but it's really something when an *instructor* can't even bother to [google a simple question](https://www.google.com/search?num=50&ei=96lPWt7HLtXijwPA2IPABg&q=site%3Astackoverflow.com+javascript+is+there+any+reason+to+keep+using+var&oq=site%3Astackoverflow.com+javascript+is+there+any+reason+to+keep+using+var&gs_l=psy-ab.3...31167.34108.0.34416.23.17.0.0.0.0.245.2006.1j9j3.13.0....0...1.1.64.psy-ab..15.0.0....0.jcrsHeINzWU). –  Jan 05 '18 at 16:36
  • Of course `var` shouldn't be banned, what if your students are working on a site that has to run pre-ES6, consult let isn't supported in IE10: caniuse.com/#feat=let – Dexygen Jan 05 '18 at 17:50

1 Answers1

0

The only time I've even considered using var in the last year is to take advantage of it being hoisted outside of a code block, particularly with conditionals. Something like this contrived example:

const oneOrZero = shouldBeOne => {
  if (shouldBeOne) {
    var test = 1
  } else {
    var test = 0
  }

  return test
}

You can replace the var in this case with let, but that always struck me as kind of messy:

const oneOrZero = shouldBeOne => {
  let test

  if (shouldBeOne) {
    test = 1
  } else {
    test = 0
  }

  return test
}

In the end, what I've done is take advantage of the ternary operator and const. This has the added advantage of not being reassignable after checking the initial conditional, which is typically my intent:

const oneOrZero = shouldBeOne => {
  const test = shouldBeOne
    ? 1
    : 0

  return test
}

TL;DR: I haven't used var in over a year. const is much preferable, and typically forces me to write better code. let is occasionally useful.

Zac Delventhal
  • 3,543
  • 3
  • 20
  • 26
  • You'll get pushback on your first example since, even though `var` is hoisted, it *appears* as though it's scoped to the block. In a simple example like that, the conditional operator as you snow is clearer, and in more complex examples, the confusion of the `var` placement is amplified. –  Jan 05 '18 at 17:12
  • As someone who got quite used to function scoping, my first instinct when reading a declaration is still that it is function scoped, not block scoped. As I get more used to block scoping, that might change how things first appear to me. In any case, yeah, the last version is the definitely clearest, and one of many examples of `const` nudging me to write better code. – Zac Delventhal Jan 05 '18 at 17:28