A variable is like a box in which you can insert value and by the time your program executes, this value remains in the memory of the computer.
If you want, you can also store it permanently. The way to create variables in each programming language can be different. But there is always a single intention behind creating variables and that is to store the value and perform some operations on it.
var pi = 3.14;
var person = "Jony Rana";
var answer = 'Yes I am!';
var varName = value;
- The name of the variable must begin with a letter, underscore or $ sign.
- Numbers (1,2,3,4,5,6,7,8,9) can also be used in variable names.
// Creating variable
// Printing variable value
document.write("Age is: "+age);
They work only in the block or function in which these variables are created. This is called their scope. If you try to access local variables outside their scope then the undefined variable error is generated.