JavaScript
In this tutorial we will learn about JavaScript data types.
Data types tells us about the type of the data that a variable holds. And depending on the type of data the operators that can work on that data is defined.
For example, if a variable holds a data of type number then we can perform arithmetic operations like addition and substractions using + and - operator.
Click here for JS Variables tutorial.
Following are the different data types available.
We use the typeof
operator to know the type of a variable.
Primitive data type are the basic data type that hold single value.
Following are the primitive data type.
If data type of a variable is boolean then it can take either true
value or false
value.
In the following example we have a boolean type variable.
var isRaining = false;
The typeof operator will give boolean.
var isRaining = false;
console.log(typeof isRaining); //this will print boolean
If a variable has null
value then it is of null data type. null means the variable is empty or not set.
In the following example we have a null type variable.
var dataBox = null;
The typeof operator will give object.
var dataBox = null;
console.log(typeof dataBox); //this will print object
In JavaScript typeof null
is object.
If a variable has undefined
value then it is of undefined data type. If a variable is not initialized with a value then it is set to undefined.
In the following example variable name will set to undefined by default.
var name;
console.log(name); //this will print undefined.
The typeof operator will give undefined.
var name;
console.log(typeof name); //this will print undefined
If a variable holds number then it is of number data type.
Number can be both positive and negative. It can have fractional part as well and can be expressed in exponential form.
In the following example we have number variables.
var i = 10; //integer number
var f = 3.14; //number with fractional part
var m = 3e9; //exponential form. 3e9 = 3000000000
var n = 314e-2; //exponential form. 314e-2 = 3.14
3e9 = 3 x 10^9 = 3 x 1000000000 = 3000000000
314e-2 = 314 x 10^-2 = 314 x 0.01 = 3.14
The typeof operator will give number.
var f = 3.14;
console.log(typeof f); //this will print number
If a variable holds a sequence of characters within single or double quote, it is called a String.
In the following example variable str is of type string.
var str = "Hello World!";
The typeof operator will give string.
var str = "Hello World!";
console.log(typeof str); //this will print string
These are special sequence of characters starting with backward slash \.
Following are some of the commonly used escape sequence.
Escape Sequence | Meaning |
---|---|
\' | Single quote |
\" | Double quote |
\\ | Backslash character |
\n | Line feed character |
\r | Carriage return character |
\t | Horizontal tab |
We use the escape sequence inside string.
For example if we want to include double inside a string which is enclosed in double quote we use the escape sequence \".
var s = "This \"word\" is in double quote.";
Output:
This "word" is in double quote.
An object is a multi value data type i.e, it can hold multiple values as a key-value pairs also known as properties of the object.
In the following example we have created a student object.
var student = {
name : "Yusuf Shakeel",
studentid : 123
};
The typeof operator will give object.
var student = {
name : "Yusuf Shakeel",
studentid : 123
};
console.log(typeof student); //this will print object
Click here for JavaScript Object tutorial.
A function is a block of code that is generally given a name. When the function is called the code inside it is executed.
We use functions to avoid rewriting the same piece of code.
In the following example we have defined a hello function.
function hello() {
console.log("Hello World!");
}
The typeof operator will give function.
function hello() {
console.log("Hello World!");
}
console.log(typeof hello); //this will print function
Click here for JavaScript Functions tutorial.
ADVERTISEMENT