{article Content Table}{title} {text} {/article}

  1. You declare variables in JavaScript with the var keyword.
  2. You can declare multiple variables at once.
  3. You can also declare a variable and assign it a value at the same time.
  4. Until you assign a value to a variable it is undefined.
  5. Local variables must be declared with the var keyword, otherwise they will become global variables.
  6. It is good coding practice, to put all declarations at the top of each script or function.
  7. A variable name cannot be one of the reserved words in JavaScript.
  8. The first letter of a variable name must be a letter or an underscore (_).
  9. A variable name cannot contain any space characters.
  10. Remember that variable names are case-sensitive.
  11. Remember that variable names are case-sensitive.
  12. Also, keep your variable names meaningful.
  13. Declare variables at the top of the function in which they are first used.

Follow the two rules below to create valid variable names:

  1. Start your variables with a letter, an underscore or a dollar sign.
  2. After that, use as many letters, numeric digits, underscores or dollar signs as you like.

Syntax

var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];

Variables are declared with the var keyword

  1. var z =2;
  2. var;
  3. var sum;


declare multiple variables

  1. var i, sum;


variable you can assign a value the time of initialization

  1. var i = 0, j = 0, k = 0;
  2. var message = "Hello World";