Regarding,
Local variables …
function foo(){var x = 1; ...}
…and Global variables, …
(global, example 1)
function foo(){x = 1; ...}
(global, example 2)
var x=1; function foo(){...}
…the only (simple) combination left is:
x=1; function foo{...}
… which I tested and noticed it also creates a global variable (like example 2), but is there any practical reason for this last example?
Note: I generally like to follow conventions and write readable code and stay away from ‘weirdness’.
It’s because JavaScript is a dynamic language. It allows you to use a variable without declaring it. You should always declare variables to eliminate confusion as you discussed. JSLint/JSHint will complain if you don’t.
Your “(global, example 1)” is wrong. that does not create a global variable.edit i was wrong. http://jsfiddle.net/awuzA/