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.
If you try to declare a variable that already maych or existsin same program, Java Script will treat it as a simple assignment statement and assign any new value in the declaration statement to the variable.If the duplicate declaration has no assignment, then nothing happens. If you try to assign a value to a non-existent or non match variable, Java Script will create the variable for you.
Here is some code creating and assigning values to a couple of variables:
|