This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cars(); | |
document.getElementById("demo").innerHTML += | |
"My first car, " + firstCarName + ", is stored in a global variable.<br/>"; | |
function cars() | |
{ | |
firstCarName = "Volvo"; // global | |
var secondCarName = "Volkswagen"; // function local | |
document.getElementById("demo").innerHTML += | |
"My second car was a " + secondCarName + ".<br/>"; | |
if (firstCarName == "Volvo") | |
{ | |
var thirdCarName = "Mercedes"; // function local | |
} | |
else | |
{ | |
var thirdCarName = "Toyota"; | |
} | |
document.getElementById("demo").innerHTML += | |
"My third car was a " + thirdCarName + ".<br/>"; | |
} |
In JavaScript there are two kinds of scope, global scope and function scope. Any local variable declared in a function, is visible everywhere in the function.
In the example above, both secondCarName and thirdCarName are local variables, only accessable within the function.
Note that, though the thirdCarName is declared inside an if-then block, this matters naught.
A gobal variable is visible everywhere. In the example above, this would be firstCarName.
Java
In contrast, in Java, scope can be within a method (sort of the equivalent of function), but also in block scope.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) | |
{ | |
String holland = "Holland"; | |
if (holland.equals("Holland")) | |
{ | |
String netherlands = "The Netherlands"; | |
System.out.println(netherlands); | |
} | |
// cannot find symbol | |
// System.out.println(netherlands); | |
System.out.println(holland); | |
} |
Therefore, netherlands is only available within the if statement.
let vs. var
Apparently, ECMAScript saw fit to provide JavaScript with a way to also support the Java-way of scope using the new let keyword.But be wary, there will probably always be situations where Java and JavaScript will display different behaviour.
With let, it is possible to create a variable that obscures the variable in the higher block. This is in contrast with Java, where a error is thrown during compiling.
Disclaimer
My experience with JavaScript is spotty at best. The intricacies of scope in Java and JavaScript are more subtle than the isolated examples described here. And the new let keyword makes the scope in JavaScript even more subtle.References
- Stackoverflow - Javascript - “let” keyword vs “var” keyword
- http://stackoverflow.com/questions/762011/javascript-let-keyword-vs-var-keyword
- JavaScript|MDN - let keyword
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
- Hangar.Runway 7 - The Javascript Guide to Objects, Functions, Scope, Prototypes and Closures
- http://hangar.runway7.net/javascript/guide