Introduction to the Data Types in JavaScript
This article contains the brief description of important and most used data types in the JavaScript from which a beginner should be friendly with.
Data Type in JavaScript
If we want to work with variables in any programming language then we should be aware of the usage of data types in it.
A user should be aware of the type of value to which he is going to store in his variable so that he can perform his operation efficiently and effectively.
This article will introduce you with the Data Type with some easy and friendly examples which are used quite often.
Note: JavaScript is a loosely typed programming language in which the data type of variables are decided automatically by the language itself which can also be changed later in the code.
Data Type | Description | Example |
---|---|---|
String | All the text format values | let name = "Vinay"; |
Number | All the integer or decimal values | let num = 10; |
Boolean | Either True or False | let answer = true; |
Undefined | A data type whose value is not initialized | let variable; |
Null | It denotes a null value | let text = null; |
Object | It contains key and value pairs | let myObj = {name: "Vinay"}; |
Types of Data Type in JavaScript
Data Types are majorly divided into two main types in JavaScript-
- Primitive Data Type
- Non-Primitive Data Type
1. Primitive Data Type
These can only store one single data in it.
Note:- All the data types except object are primitive data type like string, number, boolean etc.
a. String
A string data type is used to store textual data in it which is enclosed within the single quotes, double quotes or in backticks.
Notes:- Backticks are used when we want to use interpolation operator, which allows us to put any variable within a string.
// Example of String Data Type
let name = "Vinay"; // using double quotes
let greet = 'Good Morning'; // using single quotes
let message = `${greet} ${name} Have a great day`; // using backticks and variables inside string
b. Number
A number data type is used to store any integer (either positive or negative) or any decimal values within it.
// Example of Number Data Type
let number1 = 10; // Positive Integer
let number2 = -20; // Negative Integer
let number3 = 10.5; // Decimal Value
// Exponential Value
let number4 = 5e5; // 5 * 10 to the raised power 5
c. Boolean
It represents only two values either true or false only. We can declare a variable value is true or false only in boolean. It is quite similar as "Yes" or "No".
// Example of Boolean Data Type
let answer1 = true;
let answer2 = false;
d. Undefined
When we declare a variable but did not assigned a value to it, which means only the declaration of a variable is done but it's initialization is still pending then the value of that variable will be undefined.
// Example of Undefined Data Type
let variable;
console.log(variable) // It will print undefined onto the console
e. Null
Null is special value that represent an empty or unknown value in JavaScript.
// Example of Null Data Type
let value = null; // Null value will be assigned to value
Note: Difference between Undefined and Null Data Type
The undefined and null both have the empty value but the major difference between both is that the type of Undefined value is undefined but the type of Null is object.
It is quite shocking that the Null is treated as object in the JavaScript.
Checking the type of undefined
let value1;
console.log(typeof(value1)); // It will log undefined
let value2 = null;
console.log(typeof(value2)); // It will log object
2. Non-Primitive Data Type
This data type allows the user to store multiple values in it, which increases it's potential and complexity both. For eg: Object
Object
Object is used to store multiple values in the form of key and value pairs in it. We declare an object using the object name followed by curly braces in which the key and value pairs resides.
We can say most of the things in the JavaScript is treated as an object, even if it is an array then you will realize that it is treated as an object.
// Example of Object Data Type
let myObj = {
name: "Vinay Pratap Singh",
position: "Student",
blog: 5,
videos: 12
}
This article is created after studying the data type resource of Programmiz and MDN. So you can also check out their resource.