Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between javascript variable with var and without var? [duplicate]

What is the difference between given below two statements?

var temp = 10;
temp = 10;
like image 624
Dixit Singla Avatar asked Feb 14 '14 05:02

Dixit Singla


People also ask

What is the difference between declaring a variable with the keyword var and without the keyword var?

If var keyword is used within a function or other non-global scope then that variable's scope is not global . If var keyword is not used before a variable name, then that variable's scope is global .

How does variable behave without VAR?

If you declare a variable, without using "var", the variable always becomes GLOBAL.

Why we should not use VAR in JavaScript?

Scoping — the main reason to avoid var var variables are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } . Let's understand what that means via code. console. log(baz); // ReferenceError}run();

Can we declare variable without var in JavaScript?

Variables can be declared and initialize without the var keyword. However, a value must be assigned to a variable declared without the var keyword. The variables declared without the var keyword becomes global variables, irrespective of where they are declared. Visit Variable Scope in JavaScript to learn about it.


1 Answers

If you declare a variable with "var" within a function will be local to your function, else the js engine will start to look for the variable in the local scope (function) and if doesn't find it then will be declared in the globalspace automatically

From this link: https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-3/variable-scope

When you declare a global JavaScript variable, what you are actually doing is defining a property of the global object (The Global Object). If you use var to declare the variable, the property that is created is nonconfigurable (see Property Attributes), which means that it cannot be deleted with the delete operator.

Then if you do within a function or in the global-space (outside any function):

temp=10;

You could use it anywhere like:

console.log(window.temp);

Just a bunch of nested functions (read the code comments starting from the inner one for better understanding):

//lots of stuff here but not a "var temp=9"

//I couldn't find "x" I will make it global as a property of the globalObject
 function myFunction(){ //is x here ? no ? then look outside
    (function(){ //is x here ? no ? then look outside
        (function() {  //is x here ? no ? then look outside
                x=5; //declaring x without var, I will look for it
        }());
    }());
}

myFunction();
console.log(window.x); //As x was declared a property from the global object I can do this.

If you declare it with var within a function you can't do window.temp (variable isn't hoisted), if you do it inside a function that variable will be "local" to your function (won't be hoisted), ie:

foo = 1;
function test() {
    var foo = 'bar';
}
test();
alert(foo);

// Result: 1

Source here from above sample and others here

Also notice that using "var" in the global-space (outside) all your functions will create a global variable (a property in the window object) btw, use var always.

like image 131
Allende Avatar answered Sep 29 '22 10:09

Allende