You just use const at global scope:
const aGlobalConstant = 42;
That creates a global constant. It is not a property of the global object (because const, let, and class don't create properties on the global object), but it is a global constant accessible to all code running within that global environment.
It's probably worth noting, though, that the trend is to move away from globals, and in fact even to move away from having any code run at global scope (although top-level scripts in browsers will always have access to global scope, for compatibility).
For instance: In NodeJS, none of your code is running at global scope; everything (including the main script you run via the node command) is in a NodeJS module environment, which is not global scope. So in NodeJS, the line above wouldn't create a global, because it's scoped to the call to your module. To create a true global in NodeJS (which usually isn't necessary), you have to create a property on the global object. But how to access the global object? NodeJS calls your code with this set to an empty object (rather than the usual loose mode default where this refers to the global object), and of course there's no window in NodeJS. Answer: NodeJS provides a global available for that: global
// Define a global constant in NodeJS
Object.defineProperty(global, "aGlobalConstant", {
value: 42
});
That's a constant because by default, properties created via Object.defineProperty are read-only.
ES2015's modules take it a step further than NodeJS: You cannot create a global at all unless the environment makes a global like NodeJS's global available to you (in which case you can do the Object.defineProperty thing). By default, per spec, there is no such global; it's just environments that define them (window on browsers, global in NodeJS, ...). And the top-level code in an ES2015's module is not run with the old default of this referring to the global object (this has the value undefined at the top-level scope of an ES2015 module), so you can't use this for it.
Videos
You just use const at global scope:
const aGlobalConstant = 42;
That creates a global constant. It is not a property of the global object (because const, let, and class don't create properties on the global object), but it is a global constant accessible to all code running within that global environment.
It's probably worth noting, though, that the trend is to move away from globals, and in fact even to move away from having any code run at global scope (although top-level scripts in browsers will always have access to global scope, for compatibility).
For instance: In NodeJS, none of your code is running at global scope; everything (including the main script you run via the node command) is in a NodeJS module environment, which is not global scope. So in NodeJS, the line above wouldn't create a global, because it's scoped to the call to your module. To create a true global in NodeJS (which usually isn't necessary), you have to create a property on the global object. But how to access the global object? NodeJS calls your code with this set to an empty object (rather than the usual loose mode default where this refers to the global object), and of course there's no window in NodeJS. Answer: NodeJS provides a global available for that: global
// Define a global constant in NodeJS
Object.defineProperty(global, "aGlobalConstant", {
value: 42
});
That's a constant because by default, properties created via Object.defineProperty are read-only.
ES2015's modules take it a step further than NodeJS: You cannot create a global at all unless the environment makes a global like NodeJS's global available to you (in which case you can do the Object.defineProperty thing). By default, per spec, there is no such global; it's just environments that define them (window on browsers, global in NodeJS, ...). And the top-level code in an ES2015's module is not run with the old default of this referring to the global object (this has the value undefined at the top-level scope of an ES2015 module), so you can't use this for it.
Great answers above especially the note about "const, let, and class don't create properties on the global object".
Just to add, beware of implications of that as relates to IFrames.
In an IFrame you can access the variables defined in the parent-window (if they come from the same domain) by referring to "parent.someVar".
But because 'const' or 'let' or 'class' won't add to the properties of their global object the iFrame can not access variables defined by const or let in the parent-window. But you CAN if you have defined them as variables with 'var', in the parent window.
In that way 'var' gives you a more global reference than 'const' or 'let'. If you need to create a variable that is available in sub-frames you need to use 'var' to declare it it seems. And but then it will not be a "constant".
So one way to answer your question is to say that while you can define "global variables" by using 'var' you can not define (truly) "global constants" because if they are declared with 'const' they won't be visible in iFrames, if they are defined with 'var' they won't be constants.
Unless there's some way to access consts defined in the parent-window. I haven't found one. Is there?
If your variable is declared in the global scope, then it doesn't really matter if you use let or var.
These are functionally identical:
let myVar = 123;
function doStuff() {
console.log(myVar);
}
doStuff();
var myVar = 123;
function doStuff() {
console.log(myVar);
}
doStuff();
The difference between var and let becomes significant when you're declaring them in blocks:
if(true) {
var foo = 1;
}
if(true) {
let bar = 2;
}
console.log("var foo:", foo);
console.log("let bar:", bar);
As you can see, the let declaration is restricted to its wrapping block scope. The var declaration ignores block scope.
Looks like you are attempting to maintain a character's (hero) state in multiple locations. This will become more and more difficult to maintain in a global scope as each character's actions / states will add to the global variables.
As per @jeff-huijsmans suggestion, I believe you should maintain the state inside your game object.
This could be defined in a few ways:
game.state.firePaused- This locks your game state to a single character, but will better contain the state of a character shooting.game.hero.firePaused- This allows each character to maintain their own shooting state. This also has the added benefit of being able to add more characters with firing states.
As an aside, it looks like most of the answers here are attempting to address the scope issue. Defining variables globally and attempting to maintain a state outside of a function becomes very difficult to understand/read/test. There will be a lot of opinions on this topic. Fortunately for your root issue you can avoid this by using your pre-existing state object.
Hello, my background is java and I am trying to understand variable declaration deeper. Mainly what the differnces between let and var keywords. I found this great website for those who are still looking to understand.
https://scotch.io/tutorials/understanding-scope-in-javascript#toc-block-statements
But I am still having trouble how LET is not a global scope? If you google let vs var, you will get tons of tables like this:
But can't let be considered global too? Since I can declare it globally. Or is it referring to automatic globlazation? Where even if you declare the variable in block statement, it will automatically set it to globabal variable?
Sheesh, I have under estimated the JS. Thanks.
Keywords for people looking for this:
LET vs VAR,
GLOBAL SCOPE VS LOCAL SCOPE
Global scope vs block statement
scope vs block