In the above example x and car are declared as variables which can be used anywhere in the script. The semi colon is used to end the statement. In the above example two variables are declared using two different statements. A single “var” statement can also be used to declare multiple variables, and each variable is separated by a comma. For example:
var x, car;
The variables are assignment values using assignment operator (“=”). For example:
x = 2;
car = “ford”;
In the above example variable “x” is assigned value “2” and variable “car” is assigned value “ford”.
The variables can be declared and assigned values at the same time. For example:
var x = 2;
var car = “ford”;
As multiple variables can be declared in a single statement, they can also be assigned values using a single statement. For example:
var x = 2, car = “ford”;
The number data type can be declared to have values as integers, floating point numbers or any other number type. For example:
var x = 10;
var y = 10.23;
var z = -7;
The number will remain of the same type unless a operation is performed to change the number type. The exponential notation is used to define long numbers. For example 1.23 x 105 can be denoted as:
var biginteger = 1.23e5;
Where “e” denotes the exponential ie to represent “times 10 to the power of”.
The sequence of characters is defined as of type string. The string type may contain letters, words, whitespaces, numbers, symbols or any displayable character. The strings can be defined within single quotes ( ‘ ’ ) or double quotes ( “ “ ). For example:
var car = “ford”;
var color = ‘blue’;
Please note that if a string is open with single quote it must close with single quote and similarly if a string is open with double quote it should close with double quote. Every string opened must close with its corresponding quote.
The above code will cause errors since double quotes cannot be used directly within double quotes. Therefore, the above statement is written correctly using backslash for example:
Suppose a string containing single quote has to be displayed, then its correct to use single quote within double quotes and this will not create an error. For example:
Similarly, if a string contains a single quote, it needs to be escaped with backslash and even a backslash needs to be escaped using a backslash.
The above statement will not display string within double quotes on two different line in the web browser, since the web browser ignores newline character. Therefore, to achieve about result, we can use “<br>” tag in the above statement. For example:
The Boolean variable can have either true or false value. For example:
var flag = true;
The data type null is used for empty objects. For example:
var car = null;
Even here the null should not be enclosed within double quotes.
The data type undefined is used for variable which have not been assigned an initial value or for undeclared variables. For example:
In the above script the variable car is declared but has not been assigned any value. Therefore, car has an undefined data type.
The variables declared and assigned values can be used anywhere in the script. The variables of type string can used “+” operator to concatenate string variable or string in quotes and string variables. As explained earlier in escape character, the HTML tags or elements can also be concatenated as strings.