Should You Declare Javascript Variable with “var”?

Variables should be declared using “var”. When declared with var, it is a variable in the current scope (e.g local to the function). If not declared, js will search the variable in outter scopes, until it reaches the global space.

Variables declared with “var” can be deleted using 「delete myvar;」, but variable not declared with “var” cannot be deleted.

So, you should always declare variable with “var”, even for global variables.

See: Javascript Basics.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs