Developed By
Gautam Kumar - Full stack developer
DEEP DIVE INTO
In JavaScript, including ECMAScript 6 (ES6) and later versions, have several fundamental data types. These data types are essential for working with variables, values, and data in JavaScript.
There are two types of data types in JavaScript. Both are listed below.
Primitives
Non-primitives
The number data type represents both integer and floating-point numbers. For example: let age = 30;
String data type represents textual data. Which we generally represent within double or single quotes. For example: let name = 'Alice';
Boolean data type represents a true or false value. For example: let isStudent = true;
Undefined data type represents a variable that has been declared but hasn't been assigned a value yet. For example: let score;
Null data type represents the absence of a value or an intentional absence of an object value. For example: let data = null;
Symbol data type (ES6) represents a unique and immutable value, often used as object property keys. For example: const uniqueID = Symbol('description');
BigInt data type (ES11) represents large integer values, useful for math beyond the limitations of the Number data type. For example const bigIntValue = 1234567890123456789012345678901234567890n;
Object data type
Function data type
Array data type
Date data type
Regex data type
Map and Set data type
Let me explain each non-primitive data type one by one.
Object data type is a collection of key-value pairs that hold data, functions, and other objects. Let's look at the representation of object data type. Below person is an object data type.
javascriptconst person = {
name: 'Alice',
age: 30,
isStudent: false,
getName: function(){
return this.name;
}
};
Function data type is a specific type of object in JavaScript that provides a facility to reuse a block of code. For example:
javascriptfunction greet(name) {
console.log('Hello, ' + name);
}
The greet function can be used N number of times.
Arrays are objects that hold multiple types of values and values can be of different types as well. Let's look at below example:
javascriptconst numbers = [1, 2, 3, 4, 5, 'Rakesh', {name: 'Ritesh', age: 28}];
The date data type represents dates and times. JavaScript provides a Date object by which we can get the date and time of our system or server. For example:
javascriptconst currentDate = new Date();
const hours = currentDate.getHours();
const minutes = currentDate.getMinutes();
const seconds = currentDate.getSeconds();
RegExp represents regular expressions, that are particularly used for pattern matching and text manipulation. For example:
javascriptconst pattern = /abc[a-z]/;
The map datatype represents a collection of data in the format of key-value pairs like an object where keys can be of any data type where the object's keys are in the format of stings or symbols. For example:
javascriptconst myMap = new Map();
myMap.set('name', 'Alice');
Set represents a collection of data of unique values. For example:
javascriptconst mySet = new Set([1, 2, 2, 3, 4, 4]);