What is Variable ?
- JavaScript Variable is called as a container which can store value or expression.
- In JavaScript, Value / Expression can be assigned to variable.
- Variable can hold only one value at a time.
How to declare Variable in JavaScript ?
var variable_name;
Explanation :
- We can declare variable using “var” keyword.
- We can declare one or more variable at a time.
- We can initialize variable at the time of declaration.
- We can declare “n number” of variables as per requirement.
Different methods of declaring JavaScript variables
- Declaring One JavaScript variable
- Declaring Multiple JavaScript variable.
- Declaring and Assigning One JavaScript Variable.
- Declaring and Assigning Multiple JavaScript Variables.
Example :
// Declaring one JavaScript Variable
var sub1;
// Declaring Multiple JavaScript Variables
var sub1, sub2;
// Declaring and Assigning one JavaScript Variable
var sub1 = 78;
// Declaring and Assigning n JavaScript Variable
var sub1 = 78 , sub2 = 90;
Rules for Declaring JavaScript Variables :
- Variable name contain any Letter of the alphabet, Digits (0-9), and the Underscore character.
- Spaces,Punctuation marks and Special Symbols are not allowed.
- The first character of a variable name cannot be a Digit.
- JavaScript variable’s names are Case-Sensitive.
- For example sub1 and Sub1 are two different variables.
Valid Variable Name :
Variable Name |
Status |
Reason |
Pri |
Valid |
Capital at start allowed |
SUM |
Valid |
Variable name in CAPS is allowed |
suM |
Valid |
Combination of small and big letters is allowed |
num of element |
Invalid |
Space not allowed |
10num |
Invalid |
Digit at start is not allowed |