JavaScript variable hoisting
In JS, variables are hoisted (declared) to the top of the scope.
This means that the following function:
function fn() {
alert("Hello " + world);
var world = "world";
}
Will actually be evaluated as:
function fn() {
var world;
alert("Hello " + world);
world = "world";
}
That's why JS will not bother you about uninitialized variables, and that's why I think it's best practice to declare your variables where they will be eventually - at the top of the scope. This way you can spot problems yourself.
You could also use this: http://www.jslint.com/lint.html
The above example will alert 'Hello undefined'.
Written by Alexander Brevig
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Variable
Authors
alexanderbrevig
1.455K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#