- It is the basic unit for holding or storing the data.
- A variable value can change at any time.
- Variable must have a unique name.
var (Variable-Name) = value;
- var number = 5; // variable store numeric value
- var string = "Hello" // variable store string value
- x store the value of 9
- y store the value of 2
- z store the value of 7
- For example in c# language, we have to create an integer variable to use for numerics.
int X = 5;
- To create a string variable we use string keywords
var myVarname = 10; alert(myVarname); myVarname = "This is a string value"; alert(myVarname)Look in this above example, in the beginning, myVarname variable is treated as int numeric type and with alert. Following that, we have worked on myVarname again as a string and showed it. May you have noticed that we have not defined their data types.
var name = "Justin"; var salary; salary = 10,000;You can create a variable or declare the value of the variable later as you can see in the example. Although you can also do variable initialization too. You can declare multiple variables with the single var keyword without storing the value.
var age, height, class, name;
// initializing variable var x = 5; var name = "Adam";
// declaring single variable var name; // declaring multiple variables var age, height, class, name;
Things That You Should Keep In Mind
- Variables can only store a single value of data that can change later.
- All the variables can only be defined using a var keyword. Variables declared without var keywords becomes global variables.
- Multiple variables can be declared in a single line like var age = 10; name = "ali"; class = 3;