mercredi 22 juin 2016

Does an if statement create a local scope?

I am currently reading through some javascript tutorials and I came upon this bit of example code from here: http://ift.tt/1bDJzwG

if (hour < 18) {
greeting = "Good day";

}

In this example, the greeting variable is not prefixed with a var statement. Now I looked up what this means, and my understanding is that it causes java script to look to a higher scope, if no variable is discovered by the global scope it creates it.

So is this tutorial doing this because the if statement's block is a local environment? If so does that mean that if I were to have some code that looked like this:

if (condition){var x=10};
else {var x=5};

then there would still be no global variable x for me to call?

Aucun commentaire:

Enregistrer un commentaire