7

In the process of learning JavaScript I learned that Let and const were introduced to fix the problems of Var regarding the global scope and hoisting and not giving an error if re-declared.

Now can I write the code completely without using var ? or should I know about them for now and wait till they becomes widely "acceptable"?

In other words, for the time being should I be worried about compatibility issues if I only used let and const?

Rami Magdi
  • 104
  • 4
  • 2
    `var` still has it's purposes: http://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable. – James Donnelly Apr 04 '17 at 12:27
  • 1
    You should be worried about compatibility issues with [let](http://caniuse.com/#search=let) and [const](http://caniuse.com/#search=const) as both don't seem to be supported in IE10 and below and various other browsers. – George Apr 04 '17 at 12:29
  • 3
    @George transpilers exist ya know. Compatibility is nearly always a red-herring. – Jared Smith Apr 04 '17 at 12:30
  • @George add IE11 too..https://kangax.github.io/compat-table/es6/ though you have shims to take care of that – Suraj Rao Apr 04 '17 at 12:31
  • In ES6 var still works that way, using functions as containers, **but** there are two new ways to declare variables: `const` and `let`. `const` and `let` use `{` and `}` blocks as containers, hence "block scope". `let` works like `var` in the sense that its data is read/write. – Majid Nayyeri Apr 04 '17 at 12:35
  • 1
    few browsers support es6, you should stick with the old version and keep using var. – Karim Apr 04 '17 at 12:38
  • 4
    I don't believe that "will I run into compatibility problems?" is an opinion-based question, so voting to re-open. – Oliver Charlesworth Apr 04 '17 at 13:12
  • Well, you will *want* most variables that are explicitly in the global scope to be re-declarable. For those, `var` is still the best bet. – Bergi Apr 06 '17 at 20:40
  • use typescript and set the target syntax? write your code once, using `let` or `const`, and the ts compiler will either change it to `var` or keep it as-is depending on the target. – ps2goat Apr 06 '17 at 21:29

1 Answers1

4

To answer the question directly - no, you can't, because of the compatibility issues, as @suraj kindly reminded us.

Having said that, in the modern JS development you are increasingly unlikely to use var as let and const have clear advantages, apart from some specific uses of var and will use either BabelJS, TypeScript or even now Webpack 2 to transpile the code for backward compatibility as the production code will be shipped in vanilla JS. Modern IDEs, like WebStorm, will even lint your var's by default in ES6 mode to change them to let.

Judging from the question, you are still in the process of studying JS and you are already asking the right questions, so I would recommend to use today let and const to utilize the advantages of ES6, but that would add the complexity of dealing with the transpilers. But if you are OK with that - this is the way to go.

Oleg G
  • 141
  • 10