You can create a variable with the
var keyword, whereas the assignment operator (
=) is used to assign value to a variable, like this:
var varName = value;
The syntax of variable declaration is shown below.
var firstName="Rakesh"; var firstNumber=5; var isMarried=false //boolean
var firstName; firstName="Rajan";
Note: If a variable is declared and the value has not been assigned to it, then the variable will automatically assume undefined.
You can declare multiple variables in one line. The variables declared in one line will be separated by comma. The example of declaring multiple variables is shown below:
var firstName="Rakesh", firstNumber=5, isMarried=true;
The let and const keywords are also used to declare variables. The const keyword works same as let except for one change. The variable declared as const cannot be reassigned later but the variable declared as let can be reassigned later.
// Declaring variables let name = "Ramesh"; let age = 11; // Declaring constant const PI = 3.14; // Trying to reassign PI =52; // error