-1

If I have a global variable, and I change the variable's value inside a function, will the change be reflected outside of the function?

e.g.

var blaah="blaah";

function myFunction(){
 blaah="blaah blaah";
}

console.log(blaah);

Will the output of console.log(); be "blaah", or "blaah blaah".

Ben
  • 2,200
  • 20
  • 30
  • Why don't you give it a try? You can even run code here in StackOverflow. – elclanrs Jan 19 '15 at 01:05
  • It seems to work, this question is more if there are exceptions, or if this is not cross-browser. – Ben Jan 19 '15 at 01:08
  • 2
    The output would be `blaah`, because you didn't run the function but you have conflicting variable names. If you want to know more I'd suggest you read the [spec](http://www.ecma-international.org/publications/standards/Ecma-262.htm). – elclanrs Jan 19 '15 at 01:10
  • @elclanrs good catch, I'll fix that – Ben Jan 19 '15 at 01:15
  • The question is whether it can be ***EDITED***, not used – Ben Jan 19 '15 at 01:24

1 Answers1

1

Yes, the global variable that you defined would be changed (or edited) when you execute myFunction();. The answer to your question regarding the output of console.log(); would be "blahh" though, because your function was not run yet.

For more support on variable scope, check out reference.

Hope that helps!

Liam

Liam Bolling
  • 134
  • 1
  • 8