Variables

In JavaScript, variable names (including function names) must be valid identifiers. The strict and complete rules for valid characters in identifiers are a little complex when you consider nontraditional characters such as Unicode. If you only consider typical ASCII alphanumeric characters though, the rules are simple.

An identifier must start with a-z, A-Z, $, or _. It can then contain any of those characters plus the numerals 0-9.

Generally, the same rules apply to a property name as to a variable identifier. However, certain words cannot be used as variables, but are OK as property names. These words are called “reserved words,” and include the JS keywords (for, in, if, etc.) as well as null, true, and false.

Note: For more information about reserved words, see Appendix A of the Types & Grammar title of this series.

Function Scopes

You use the var keyword to declare a variable that will belong to the current function scope, or the global scope if at the top level outside of any function.

Hoisting

Wherever a var appears inside a scope, that declaration is taken to belong to the entire scope and accessible everywhere throughout.

Metaphorically, this behavior is called hoisting, when a var declaration is conceptually “moved” to the top of its enclosing scope. Technically, this process is more accurately explained by how code is compiled, but we can skip over those details for now.

Consider:

  1. var a = 2;
  2. foo(); // works because `foo()`
  3. // declaration is "hoisted"
  4. function foo() {
  5. a = 3;
  6. console.log( a ); // 3
  7. var a; // declaration is "hoisted"
  8. // to the top of `foo()`
  9. }
  10. console.log( a ); // 2

Warning: It’s not common or a good idea to rely on variable hoisting to use a variable earlier in its scope than its var declaration appears; it can be quite confusing. It’s much more common and accepted to use hoisted function declarations, as we do with the foo() call appearing before its formal declaration.

Nested Scopes

When you declare a variable, it is available anywhere in that scope, as well as any lower/inner scopes. For example:

  1. function foo() {
  2. var a = 1;
  3. function bar() {
  4. var b = 2;
  5. function baz() {
  6. var c = 3;
  7. console.log( a, b, c ); // 1 2 3
  8. }
  9. baz();
  10. console.log( a, b ); // 1 2
  11. }
  12. bar();
  13. console.log( a ); // 1
  14. }
  15. foo();

Notice that c is not available inside of bar(), because it’s declared only inside the inner baz() scope, and that b is not available to foo() for the same reason.

If you try to access a variable’s value in a scope where it’s not available, you’ll get a ReferenceError thrown. If you try to set a variable that hasn’t been declared, you’ll either end up creating a variable in the top-level global scope (bad!) or getting an error, depending on “strict mode” (see “Strict Mode”). Let’s take a look:

  1. function foo() {
  2. a = 1; // `a` not formally declared
  3. }
  4. foo();
  5. a; // 1 -- oops, auto global variable :(

This is a very bad practice. Don’t do it! Always formally declare your variables.

In addition to creating declarations for variables at the function level, ES6 lets you declare variables to belong to individual blocks (pairs of { .. }), using the let keyword. Besides some nuanced details, the scoping rules will behave roughly the same as we just saw with functions:

  1. function foo() {
  2. var a = 1;
  3. if (a >= 1) {
  4. let b = 2;
  5. while (b < 5) {
  6. let c = b * 2;
  7. b++;
  8. console.log( a + c );
  9. }
  10. }
  11. }
  12. foo();
  13. // 5 7 9

Because of using let instead of var, b will belong only to the if statement and thus not to the whole foo() function’s scope. Similarly, c belongs only to the while loop. Block scoping is very useful for managing your variable scopes in a more fine-grained fashion, which can make your code much easier to maintain over time.

Note: For more information about scope, see the Scope & Closures title of this series. See the ES6 & Beyond title of this series for more information about let block scoping.