The difference between these two lines of code?
a = 'abc';
var b = 'abc';
Are they just different variables? is that it?
I want to say it is but I am just learning.
The difference between these two lines of code?
a = 'abc';
var b = 'abc';
Are they just different variables? is that it?
I want to say it is but I am just learning.
The first one implicitly creates a global variable and the second one creates a variable in the current scope.
It depends.
On global scope wise there is no difference. However, if you are on local scope there is a difference.
//Both global
var test1=1;
test2=2;
function first()
{
var test1 =-1; // Local: set a new variable independent of the global test1
test2 =3; // Change the test2 global variable to 2
console.log(test1); //will display -1 (local variable value)
}
function second()
{
console.log(test1); //will display 1 (global variable value)
}
Inside function first()
the value of test1 is -1 because we test1 is hitting the local variable created using var
, function second()
has no test1 as a local variable so it will display 1.