- 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
In this example, we have declared two variables using var: number, string. we have assigned each variable a different value. The data value of the number is ‘5’ and string data value is ‘Hello”. Let’s have a look at few other examples:
In this picture, you can see that
- 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
String str = “Learning”
var myVarname = 10;
myVarname = “This is a string value”;
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.
Storing the value of a variable is called variable initialization. You can store the value of the variable at the time of variable creation(in other word declare) or later when you need to call that variable.
var name = “Justin”;var salary;salary = 10,000;
You can declare multiple variables with the single var keyword without storing the value.
var age, height, class, name;
// initializing variablevar x = 5;var name = “Adam”;
// declaring single variablevar name;// declaring multiple variablesvar age, height, class, name;
Types of Variables
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;
I have explained every point in detail. We hope you understand it clearly. But if you have any question related this topic you can comment below. We will reply to your message.