All programming languages contain built-in data structures, however they frequently vary between languages. The built-in data structures that JavaScript offers, together with a description of their features, are listed in this article. Further data structures can be created using these.

JavaScript has eight data types. Such as 

1. String

2. Number

3. Bigint

4. Boolean

5. Undefined

6. Null

7. Symbol

8. Object


Among all of this object data type contain few types: Object, Array and Date.

Let's see the details above of the data types:


String Data Type

Textual data is represented by the string data type (i.e. sequences of characters). As demonstrated below, strings are made by enclosing one or more characters in single or double quotations.


// using single quotes
var single = 'Its single quote string!';   
// using double quotes
var double = "Its Double quote string!";  
// single quote inside double
var a = "Let's play a game.";  
// double quotes inside single quotes
var both = 'Raihan said "Hello" and Tuktuki.';  
// escaping single quote with backslash
var backslash = 'We\'ll always play.';



Number Data Type

The number format represents positive or negative numbers, without a decimal number or exponential notation. 

Let's see an example : 

// integer
var integer = 50;      
   
// floating-point number   
var floating-point = 50.5;  
     
// exponential notation, same as 5.25e6 or 5250000
var exponential = 5.25e+6;


BigInt Data Type

All integers in JavaScript are kept in a 64-bit floating-point representation. A new datatype (ES2020) called JavaScript BigInt can be used to store integer values that are too large to fit into a standard JavaScript Number.

Let's see an example : 

let bigInt = BigInt("123456789012345678901234567890");


Boolean Data Type

Boolean data types represent only two values : true or false

Let's see an example : 


var one= 10,
       two = 15,
      three = 20;
alert(two> one) // Output: true
alert(two > three) // Output: false


Undefined Data Type

The unique value undefined is the only possible value for the undefined data type. A variable's value is undefined if it has been declared but not given a value.

Let's see an example : 

var first;
var second = "Hello World!"
alert(a) // Output: undefined
alert(b) // Output: Hello World!


Null Data Type

The unique value "undefined" is the only possible value for the undefined data type. A variable's value is undefined if it has been declared but not given a value.

Let's see an example: 

var first = null;
alert(first); // Output: null
var second = "Hello Raihan!"
alert(second); // Output: Hello Raihan!
second = null;
alert(second) // Output: null


Object Data Type

The object is a sophisticated data type that enables the storage of data collections.

An object has attributes, which are described as key-value pairs. While the value of a property can be any data type, including strings, numbers, booleans, or sophisticated data types like arrays, functions, and other objects, the property key (name) is always a string. In following chapters, you'll gain additional information about items.

You can see the easiest approach to build an object with JavaScript in the example below.

Let's see an example: 

var object = {};
var car = {"name": "Prado", "brand": "Prado", "color": "Black"};
// For better reading
var car = {
    "name": "Prado",
    "brand": "Prado",
    "color": "Black"
}


Array Data Type

An object type called an array is used to store several values in a single variable. A value (also known as an element) in an array can be any data type, including numbers, strings, booleans, functions, objects, and even other arrays. Each value in an array has a position in space-time known as its index. The initial array element is arr[0] rather than arr[1] since the array index starts at 0.

As seen in the example below, the simplest way to create an array is to define its elements as a comma-separated list surrounded in square brackets:

Let's see an example: 

var fruits = ["Mango", "Apple", "Banana"];
console.log(fruits[0]);   // Output: Mango
console.log(fruits[1]);   // Output: Apple
console.log(fruits[2]);   // Output: Banana



Function Data Type

The function runs a block of code and is a callable object. It is possible to assign functions to variables because they are objects, as demonstrated in the example below:

Let's see an example: 

var test = function(){ 
    return "Hi Raihan!"; 
}
// Check data type of test variable
alert(typeof test) // Output: function
alert(test());     // Output: Hi Raihan!


In actuality, functions can be utilized anywhere a different value can be. Arrays, objects, and variables can all store functions. Functions can be returned from other functions and can be supplied as arguments to other functions. Think about the subsequent function:

function person(name){
    return "Hello " + name;
}
function displayPerson(displayFunction, name){
    return displayFunction(name);
}
var result = displayPerson(person, "Peter");
alert(result); 
// Output: Hello Peter


Hope this article will help to understanding about JavaScripts data types.


Share