11

What is the difference between given below two statements?

var temp = 10;
temp = 10;
3
  • 4
    it's a scope prefix. var makes local, without makes global or error under strict. Commented Feb 14, 2014 at 5:42
  • @dandavis -- If I will use " temp = 10; " inside the function then it would be global or would be local to that function? Commented Feb 14, 2014 at 5:46
  • 1
    @DixitSingla Apparently, global : function f(){temp=1}; f(); temp; // 1. Commented Feb 14, 2014 at 5:58

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This code will output in 9 (console.log('in', temp)) (console.log('out', temp)) will say temp not defined it will never execute " function x "
temp is declared in the anom function, it works inside since you're self-executing the function, outside can't work because isnt' declare it, function x() is never called, then temp isn't modified, if you do x() temp will be 10 for the first console.log since "find" temp in the previous scope (from the anom function)
0

When you define the variable at the global scope, then you can access it anywhere. If you redefine it using it var, then the variable has that value only in the current scope.

Define a variable in the global scope:

var a = 1;

Now you can access it via a function's scope like this:

function print() {
  console.log(window.a); // 1
  window.a = 5;
  anotherFunction(); // 5 
  var a = 3;
  console.log(a); // 3
}
function anotherFunction() {
  console.log(a); // 5;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.