Reputation: 1144
In browser console if I type var x=10
it shows undefined
while if i type x=10
it shows 10
. Both perform the same task then what is the difference?
I am not asking difference between using var and not using var?
Upvotes: 6
Views: 7096
Reputation: 11
Some more information on the difference apart from mentioned above:
var x=10;
x=10;
There is a difference in terms of property descriptor which gets created
when you write var x=10;
you can't delete x
because configurable will be false for this x
On the other hand x=10;
you can delete x
because configurable will be true for this x
e.g.
var a="UnDeletable";
delete a; //Prints false
console.log(a);//Prints UnDeletable
Open new browser console don't write below statement in the same console otherwise it will use same a used before and same property descriptor will be used i.e. configurable false
a="Deletable";
delete a;//Prints true
console.log(a);//Reference error..a is not defined
Upvotes: 0
Reputation: 2373
Scenario 1 :
See Basically when you are typing x = 10 on browser console you're getting 10 because 10 is an int value being returned by browser console.
Scenario 2 : And When you are typing var x = 10 on browser console you're getting "undefined" because it displays the return value of each command and console doesn't return anything in this scenario.
And Further For more Reference Visit Here :
http://blog.modulus.io/absolute-beginners-guide-to-nodejs
Upvotes: 1
Reputation: 2896
EXPLANATION
case x = 10
:
This creates a variable in the Global scope named x
with value 10
.
Additionally, this is an expression, which returns the value 10
. This is useful in order to be able to do things like var x = y = 10;
which sets both x
and y
to the value 10
case var x = 10
:
This creates a variable in the current scope, which just so happens to be
the global scope, named x
with value 10
. Because it is created with the
syntax var
, it cannot be evaluated as an expression, therefore it returns undefined
, which is printed to the console.
SUMMARY
There is no difference in effect of writing var x = 10
vs x = 10
from the console, although there will be in other places. The latter is also not allowed in strict mode. However the first returns undefined
because when run, there is no output, however the second returns 10
because x=10
is an expression.
EXAMPLE
You can see what is happening a little better if you use eval
var output = eval('x = 10');
console.log(output) // 10
vs
var output = eval('var x = 10');
console.log(output) // undefined
Upvotes: 3
Reputation: 377
// they both define var x and set it value to 10,but they are diff about below
// x is globally
x = 10
// x is scopely
var x = 10
In browser env, globally mean var is define as the Object window
's property
But be careful about using the first code, it can cause global define variable.
And if it in strict mode
,this will not work, because the Implicitly defined global is not allow.
Upvotes: 0
Reputation: 2012
You are in browser console, so you are alerady in global scope, and with or without var make no difference on how the variable was stored:
However, =
is a operator which returns the value you assigned, so a = 1
will evaluate to 1, and you see a 2
when you typed b = 2
. var
don't return anything, it is a statement not an expression.
Upvotes: 3
Reputation: 2469
If var is used within a function or other non-global scope then the variable is not a global variable. If var is not used before a variable name, then you have created a global variable.
/
/ These are both globals
var foo = 1;
bar = 2;
function()
{
var foo = 1; // Local
bar = 2; // Global
// Execute an anonymous function
(function()
{
var wibble = 1; // Local
foo = 2; // Inherits from scope above (creating a closure)
moo = 3; // Global
}())
}
If you're not doing an assignment then you need to use var:
var x; // Declare x
Upvotes: 1
Reputation: 4819
var x = 10; sets the value for the current scope( inside function as example).
x = 10; sets the value for the global scope, so it's accessible everywhere.
Upvotes: 0