Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some reasons to use var instead of let in javascript? [duplicate]

With the new keyword let for declaration of variables in javascript ES6, I can no longer think of good reasons to use var. So far, I have been doing just that and I do not see any disadvantage of using let ALL THE TIME.

What are good reasons to use var today? Is it a good practice to use let all the time today?

like image 706
guagay_wk Avatar asked Dec 08 '16 03:12

guagay_wk


People also ask

Why would you use VAR instead of let?

let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Which is better VAR or let in JavaScript?

var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped. Variable declared by let cannot be redeclared and must be declared before use whereas variables declared with var keyword are hoisted.

Why do people still use VAR in JavaScript?

For you question directly, var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet. The only real advantage to var is it's compatibility. If you are writing for old platforms like .

When should you use VAR in JavaScript?

Always declare JavaScript variables with var , let , or const . The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browsers, you must use var .


1 Answers

IMO there is no definite advantage of using var over let, other than for its scope. One reason might be to support older browsers perhaps (if you don't plan to use an ES6 to ES5 compiler like Babel).

like image 181
Divyanshu Maithani Avatar answered Oct 03 '22 01:10

Divyanshu Maithani