{article Content Table}{title} {text} {/article}
- You declare variables in JavaScript with the var keyword.
- You can declare multiple variables at once.
- You can also declare a variable and assign it a value at the same time.
- Until you assign a value to a variable it is undefined.
- Local variables must be declared with the var keyword, otherwise they will become global variables.
- It is good coding practice, to put all declarations at the top of each script or function.
- A variable name cannot be one of the reserved words in JavaScript.
- The first letter of a variable name must be a letter or an underscore (_).
- A variable name cannot contain any space characters.
- Remember that variable names are case-sensitive.
- Remember that variable names are case-sensitive.
- Also, keep your variable names meaningful.
- Declare variables at the top of the function in which they are first used.
Follow the two rules below to create valid variable names:
- Start your variables with a letter, an underscore or a dollar sign.
- 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
- var z =2;
- var;
- var sum;
declare multiple variables
- var i, sum;
variable you can assign a value the time of initialization
- var i = 0, j = 0, k = 0;
- var message = "Hello World";