Data types are used to specify what kind of data can be stored in the variable and manipulated within a program. Data types in JavaScript are divided into three main categories:
The string data type is used to represent textual data i.e. a single character or sequences of characters. String data types hold data within single or double quotes.
Example:
var a = ‘NIELIT’; // uses single enclosing quotes
var b = "NIELIT"; // uses double enclosing quotes
We may also include the quotes within the enclosing quotes inside the string as long as they don't match the enclosing quotes.
Example:
var a = "Let's attend the JavaScript Class"; // single quote inside double quotes
var b = 'Say "Hello" and wait for response'; // double quotes inside single quotes
var c = 'Ajay\'ll never attend the class'; // escaping single quote with backslash
The number data type in JavaScript is used to represent positive or negative numbers with or without decimal place, or numbers written using exponential notation e.g. 1.7e-12 equivalent to 1.7x10-12.
Example:
var a = 15; // integer
var b = 25.5; // floating-point number
var c = 4.27e+6; // exponential notation same as 4.27e6 or 4270000
var d = 4.27e-6; // exponential notation, same as 0.00000427
Infinity, -Infinity and NaN are the special values used with Number data type where-
Example:
26 / 0; // Output: Infinity
-26 / 0; // Output: -Infinity
26 / -0; // Output: -Infinity
“NIELIT” / 2; // Output: NaN
“NIELIT” / 2 + 10; // Output: NaN
math.sqrt(-2); // Output: NaN
The Boolean data type has only 2 output values: true or false. Boolean data types stores values like Yes (true) or No (false), On (true) or Off (false), etc. Boolean values may come as a result of comparisons in a program.
Example:
var a = 3;
var b = 7;
var c = 17;
(b > a); // Output: true
(b > c); // Output: false
An array is nothing but a type of object used for storing multiple values in single variable. Each value in an array is also known as element. All these elements has a numeric position called its index starting from 0, i.e. first array element is arrayname[0].
Elements or values in an array may contain data of any data type-numbers, strings, booleans, functions, objects, and even other arrays. Arrays can be created by specifying the elements separated comma and enclosed in square brackets.
Example:
var course = ["O level", "Java", "Python"];
var state = ["UP", "Bihar", "MP"];
where,
course[0] represents “O level”
state[1] represents “Bihar”
The function executes a block of code and is is callable object. It is possible to assign functions to variables since functions are objects.
Example:
var welcome = function(){ return "Welcome to NIELIT"; }
However, the correct way to write a function is as follows:
function()
{
return "Welcome to NIELIT";
}
Functions may or may not have a return statement. In order to understand JavaScript Functions in detail, click here.
In the example, the return value of the function which is a string, will be assigned to the variable welcome.
In JavaScript, we may have variables which can be used as containers for storing data values. These variables may further used to display data. A variable in JavaScript can be defined with var keyword.
Example:
var a; var temp; var c;
var temp = a + c;