JavaScript – Variables

JavaScript – Variables

JavaScript Variables

Variables are important part of any programming language. A variable represents a value or is a container which holds a value. A variable is assigned a place in memory. The value of variable can be changed any time. The variables are declared first and then assigned a value. In Javascript to declare a variable, “var” keyword is used. For example:

var x;

var car;

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;

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 variable names in Javascript are case sensitive, therefore “car”, “CAR” and “Car” are all different variables. In javascript the variable name must begin with a letter or underscore ( _ ) or dollar character ( $ ). The other characters in the variable name can be letters, numbers and underscore. The variable name cannot begin with a number. The reserved words or keywords cannot be used as variable names.

 

Data Types

The type of values that a variable can hold can be of several data types. The data type allowed in Javascript are number, string, Boolean, null and undefined. In Javascript there is no need to declare the type of variable when it is defined. Javascript allows value of any type to be assigned.

 

Number

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”.

 

Strings

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.

 

Escape Characters

Javascript allows to escape certain characters. The escape sequence uses backslash character ( \ ) to precedes the character that needs to be escaped. For example:

document.write(“This is “JavaScript” code”);

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:

document.write(“This is \“JavaScript\” code”);

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:

document.write(“This is ‘JavaScript’ code”);

 

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.

 

There are other special characters in JavaScript such as “\t” for tab, “\r” for carriage return and “\n” for newline character. Using newline (“\n”) newline character in javascript is tricky if the output is to be displayed on the web browser. For example:

document.write(“This is \nJavaScript code”);

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:

document.write(“This is <br/>JavaScript code”);

 

Boolean

The Boolean variable can have either true or false value. For example:

var flag = true;

Please note that true and false values do not have to be enclosed in double quotes. These values are defined as literals and available in Javascript.

 

Null

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.

 

Undefined

The data type undefined is used for variable which have not been assigned an initial value or for undeclared variables. For example:

var car;

document.write(car);

In the above script the variable car is declared but has not been assigned any value. Therefore, car has an undefined data type.

 

Using variables

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.