-3

Apologies if this is a duplicate. Feel free to direct me if it is.

But WHY Is this the case??

var a = 45; 
function temp() { 
    b = 34;
}

alert(b) //34

var a = 45; 
function temp() { 
    a = 34;
}

alert(a) // 45 WHY IS THIS 45??? 

Shouldn't 34 be assigned globally and then var a is assigned to the global scope initially so 34 overwrites 45?

Filburt
  • 17,626
  • 12
  • 64
  • 115
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124

3 Answers3

2

Since you didn't invoke the method:

var a = 45; 

function temp() { 
    a = 34;
}

temp(); // run temp
alert(a); //34 
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
1

I've added comments to your code inline to show what's happening:

var a = 45;            // declare a global variable
function temp() {
    b = 34;            // declare a unscoped / global variable (you're missing the "var" in your declaration
}

alert(b);              // attempts to print "b" but "temp" function hasn't been declared. Results in undefined

var a = 45;            // redeclare does nothing
function temp() {      // overrides previous declaration of "temp" function
    a = 34;            // update the global variable - when the function is called
}

alert(a);              // print the function. It's 45 because you haven't called temp()

If b=34 had been written as var b=34; then when you try to access b you'd get an undefined error. This is the reason that you should usually run your code through a JSLint check before going to publish. It checks for undeclared variables and other common mistakes. You can also force your browser to be more rigorous in its checks by using strict mode

http://www.jslint.com/

Edit:

As noted by @Spencer Wieczorek the temp function that contains the variable declaration is never actually called. Because of this, the variable is never defined (in any scope) so when attempting to access it afterwards, it will always return undefined


In this example:

var a = 45;
function temp() { 
    a = 34;
}
alert(a);

the reason the value of a doesn't change is that the temp function was never executed. There are a couple of ways you can change this. The easiest is just to call temp

var a = 45;
function temp() { 
    a = 34;
}
temp();              // call temp()
alert(a);            // prints "34"
jasonscript
  • 6,039
  • 3
  • 28
  • 43
  • 1
    *"it's not undefined because it was declared globally above"*, no it wasn't. The function was never called ( and is redefined anyways ) so the variable `b` should not be defined. At least that's what happens in this [fiddle](http://jsfiddle.net/u250q1yL/). Could you explain why you think it is? – Spencer Wieczorek Jul 28 '15 at 02:56
  • @SpencerWieczorek good catch. I've updated my answer. I hadn't noticed that the `temp` function wasn't even called – jasonscript Jul 28 '15 at 03:05
-1

Variable's value depends upon declaration of its memory. Javascript can store different memory space with same name, if they are declared in different functions. So make sure you use the right variable in the required function.

John Kiran
  • 79
  • 1
  • 14