Global Scope and functions

I am doing FCC’s quiz #169, and here is something I don’t understand.

Let’s just say that I have these two variables.

var thisVar = 10;

function thisFunction( ){ thatVar=5; }

If any variables that does not have a var in front of it is a global variable, then what about thatVar? It was declared inside a function , but without the proper var in front of it. Is it a global variable or a local one?

It depends.
If you try to call thatVar without calling thisFunction beforehand, it’s an error. If you do call thisFunction it’ll create a global variable thatVar.

You should generally use ‘strict mode’, so this kind of code will throw error.

Strict mode means no matter what add the var ahead of any variables you declare?

Strict mode means you can’t use variable without declaring it.

https://docs.microsoft.com/en-us/scripting/javascript/advanced/strict-mode-javascript

And it’s 2017, use const and let

1 Like