The variables
are temporary storage places and their values can be changed during the
script execution. Some facts about the variable in JavaScript are:
- Variables
in JavaScript are case sensitive. Age and age are considered two different
variables.
- In JavaScript
variable names must begin with a letter or the underscore character.
It can contain letters, digits, and underscores
- Variable
in JavaScript cannot contain spaces.
- All variables
can be declared with var keyword. However, during declaration
the type of variable need not be specified as they take the type according
to the value assigned to them. You can leave a variable without assigning
a value for later use.
JavaScript
supports three types of major variables such as:
·
Text: The text variables contain string values or character values
enclosed within apostrophes. Example:
//
Declaration
var
name;
//Multiple
variables can be declared in a single step
var
age, name, address, gender, salary;
//
Variable assignment
name
= “Michel”;
|
·
Numbers: Numeric variables are specified as numbers without apostrophes.
JavaScript sees all numbers as floating point numbers. It does not support
integers, octal, or hexadecimals at lower level. Example:
var
num1= 5;
var
num2 = 12.55;
|
·
Boolean: Boolean variables can contain only two values such as
true or false, yes or no and so on. Example:
Note
You
can leave a variable undefined if you don't want to assign it an initial
value. However, a better approach would be to assign the variable a null
value to avoid any confusion in the code. Example:
|