This might be a dumb questions, but I'll ask it anyway. Is there a real difference between the 2 options that I should avoid using the latter when programming in Node.js?
Asked
Active
Viewed 3,860 times
2
-
4The former creates a local variable, while the latter creates a global variable. If you use strict mode, only `var x = 1` would work. – Blender Jun 09 '15 at 08:07
-
If you are using 'use strict' in your js file then later one will give error. you cannot use undefined variable.Better use 'use strict' to avoid such circumsatnces. – shreyansh Jun 09 '15 at 08:08
-
@shreya, Thanks for your comment. But, what is use_strict, and how to use it? – securecurve Jun 09 '15 at 08:22
-
just go through it http://www.w3schools.com/js/js_strict.asp – shreyansh Jun 09 '15 at 08:28
1 Answers
5
'var x = 3' will create a variable within the current scope. Given this is declared in a function, x will not be available outside it, unless explicitly returned.
'x = 3' will create a variable within the global scope. Thus, any other code can access and alter its value. It's generally a bad practice to use variables in a global scope.

Rkn
- 122
- 2
- 11
-
Thanks Rkn. What if I use something like this: var x=1,y=2,z=3; ... Does this mean only x is a local scope or all of the variables? – securecurve Jun 09 '15 at 08:21
-
1
-