In programming, a variable is a value that can be changed. All variables will have a storage location and a name. The variable name is usually used to refer
//you can declare multiple variables as follows:
var b, c;
Assigning a value
A value is assigned to a variable using the = operator.
var a = 10;
var b = 10.5;
var c = "John";
var a = 10; //A variable is declared and initialized.
var b; //A variable is declared but not initialized.
b = "John"; //Variable is initialized.
Things to remember
- An uninitialized variable variable will have the value undefined.
- A variable will not lose it’s value if it is declared again.
- Never use quotes for storing a numerical value.
Numbers as strings
If you put any numbers in quotes, the remaining numbers will be treated as
var a = "10" + 5;
var b = 5 + 2 + "3";
var c = "The value of a is: "+ a + " and the value of b is: "+ b;
document.getElementById("txt").innerHTML = c;
The value of a is: 105 and the value of b is: 73