Blog

Identifiers in JavaScript

Names of Variables
The rules for naming of  Identifiers(Variables ,Methods and Classes) is basically the same as in other languages.
Names can begin with an alphabet, $ or _.

  1. All these are legal names:  $a=10, $_a=1, _$a1,a1, ab1cd etc.
  2. Keywords cannot be names but can contain keywords.
    Thus char. class etc is not allowed.
    However classname, charname, nameofchar etc is allowed.
  3. JavaScript variables are case sensitive. V1 and v1 are two different variables.
  4. JavaScript variables can contain objects, functions or primitive values.

Scope of Variables

Scope is the range of a program within which a variable name is valid and its value can be accessed. JavaScript has two scopes global and local.
A variable declared outside of all functions is global. A variable declared inside a function but not prefixed with var is also global.
The scope of JavaScript variables is not the curly braces block as in other languages but only global and the function.

Some examples:
of valid and invalid variables in this page.
Valid and Invalid Variables