Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason not to abandon "var"?

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?

like image 989
Rami Magdi Avatar asked Apr 04 '17 12:04

Rami Magdi


1 Answers

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.

like image 182
Oleg G Avatar answered Sep 28 '22 22:09

Oleg G