Variables

Variables are usually declared in JavaScript with the var keyword.

          var price; 

        
Note:

JavaScript is an untyped language, you cannot declare a variable to be a string or number. Variables can hold any type and data types are converted automatically behind the scenes. See Dynamic Data Types

Tip:

If you don’t use var the variable will be declared as global. You should avoid using global variables as they can result in unwanted side effects and are a frequent source of bugs! See Variable Scope.

After a variable is declared its value is undefined.

A value is assigned to a variable with the equals sign.

          price = 500; 

        

A variable can be assigned a value when it is declared.

          var price = 500; 

        

A variable can be emptied by setting its value to null.

          price = null; 

        

If you re-declare a variable, the variable will not lose its value.

          var travelType = "Car";
var travelType; // travelType is still "Car" 

        
Important:

If you assign a value to variable that has not been declared with var, the variable will automatically be declared as a global variable. See Variable Scope.

Variables names must start with a letter or underscore and cannot use any Reserved Words.

Tip:

Use short names for variables which you use only in nearest code.

Multi-word names add precision from right to left, adjectives are always at the left side.

Use camel-case.