I'm taking a Java course and all declarations use block scope( int, double, boolean, etc.
). In JavaScript however var
is function scope.
let
came along in ES6 and gave JS developers block scope. I'm adjusting my coding style and am opting to get rid of var
all together.
Is this O.K.?
I'm taking a Java course and all declarations use block scope( int, double, boolean, etc.
). In JavaScript however var
is function scope.
let
came along in ES6 and gave JS developers block scope. I'm adjusting my coding style and am opting to get rid of var
all together.
Is this O.K.?
Share Improve this question asked May 14, 2018 at 17:00 user9723618user9723618 10- 6 Yes, even google took it out in its style guide. Google JS Style Guide – Gerardo BLANCO Commented May 14, 2018 at 17:02
-
4
Yes, if you decided to don't support older browser or if you are using a transpiler. In ES6, there's no any reason to prefer
var
in place oflet
orconst
. – Christian Vincenzo Traina Commented May 14, 2018 at 17:02 - It depends on use. What benefit you will get after removing all the var?Do you have unit test case covered.Will you able to check that after replacing nothing is breaking your code – brk Commented May 14, 2018 at 17:02
- 3 For fun, note the difference between java and javascript – random_user_name Commented May 14, 2018 at 17:04
-
The only useful application of
var
is that a global can be redefined in global scope multiple times without causing an error. Can happen if there are multiple scripts that define a global, but only last definition should take effect. – Estus Flask Commented May 14, 2018 at 17:15
2 Answers
Reset to default 11Is this O.K.?
Mostly; possibly entirely. There are very few situations where you would want to use var
instead of let
/const
for technical (rather than style) reasons:
If you want to declare a global variable that bees a property of the global object (global
let
,const
, andclass
create globals, but they don't bee properties of the global object).Of course, you could use
this.varName = ...
instead ofvar varName
, sincethis
at global scope¹ is the global object.If you want to have several files where any of them may be the "first" (but not last) to define the same global, as is mon in older module formats, e.g.:
var MyApp = MyApp || {}; MyApp.something = /*...*/;
That works because the
var MyApp
part is silently ignored ifMyApp
is already declared; the equivalent structure withlet
would fail because it cannot be used if the identifier is already declared in the current scope.Of course, you're probably using a new module format by now. :-) And if you aren't, again you could do
this.MyApp = this.MyApp || {}; MyApp.something = /*...*/;
at global scope.¹
[There used to be a minor performance benefit to using var
instead of let
in a function where it was used as a loop counter. Recent versions of modern browsers all-but-remove that minor benefit (and it was minor anyway).]
¹ Note that top-level code in a module (ES2015 module, Node.js module, most bundlers...) is not at global scope. It's at module scope. Also note that at the top level of an ES2015 module, this
has the value undefined
.
If you are only targeting platforms that support ES6, adopt let and const. Stop using var.