0

Possible Duplicate:
How to declare a global variable in a .js file

I need to set a variable in a script I'm developing as global. How do I do that? Thanks in advance!

Community
  • 1
  • 1
Nathan2055
  • 2,283
  • 9
  • 30
  • 49

4 Answers4

7

Declare it in the global execution context (not in the scope of a function):

var x = "hello";

Declare it as an implicit property of the global object (be careful, people may think you've made a mistake and missed the var, and this will throw a reference error in strict mode, so don't use it):

x = "hello";

Declare it as an explicit property of the global object:

window.x = "hello";

Keep in mind that window is specific to the browser environment. If you are working with node a global object that is available in all contexts is global:

global.x = "hello";
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 1
    +1 for setting property of global object explicitly. – Tadeck Sep 09 '12 at 22:27
  • `process.x`? Did you mean `global.x`? – gray state is coming Sep 09 '12 at 23:03
  • @graystateiscoming - I don't know, I haven't used node much. Someone else edited that in. Looking at the docs, I would agree with you. – James Allardice Sep 09 '12 at 23:04
  • Weird. Yeah, the `process` object is available globally, but it isn't the global object. The global object is `global`, which would be equivalent to using `window` in a browser. – gray state is coming Sep 09 '12 at 23:05
  • 1
    @graystateiscoming - Thanks, I'll update it. I keep meaning to spend time playing around with node, but never seem to find the time! – James Allardice Sep 09 '12 at 23:06
  • @JamesAllardice—good answer. It might be picky, but the phrase "declare it as an explicit property" should be "assign to it as a property", since assignment is quite different to declaration (e.g. when the property is created). The global object is available in all ECMAScript implementations as `this` in global code, so `var global = this;` or even `var global = global || this` then assigning to `global` should work everywhere, including Node.js. – RobG Sep 10 '12 at 00:23
  • @RobG - Thanks. Very good points. I intended to update the answer at some point in the near future with the full details of what's actually going on in all 3 examples from the ES5 spec, so I'll incorporate your suggestions then too. – James Allardice Sep 10 '12 at 00:34
1

Just define a variable outside any function:

var myGlobalVariable = 42;

Just don't go overboard on global variables as it will make your code harder to read and debug.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
0

Like this

<script>
var myGlobal = 2;


function xyz()
{
  //can access myGlobal here
}


</script>
codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • 1
    While this is correct, it's a poor practice to declare global variable this way. What is usually recommended is that you explicitly do it with `window.myGlobal = 2;`. It's a lot more clear that you are defining something global and not accidentally leaking something in the global scope. – HoLyVieR Sep 09 '12 at 22:19
  • @codingbiz I think that your code is correct and isn't a poor practice, so you shouldn't have a -1. Then, you have my +1 – Oriol Sep 09 '12 at 22:42
  • @Oriol I can't see much difference from mine and [Lee Taylor]'s answer but I got -1. Thanks. :) – codingbiz Sep 09 '12 at 22:46
-1

There's a good article on Javascript scope here - basically anything defined outside is accessible inside, but not vice versa.

Community
  • 1
  • 1
Philip Whitehouse
  • 4,293
  • 3
  • 23
  • 36