What are the different data types present in JavaScript?


There are two types of data types in JavaScript:

  • Primitive data types
  • Non- Primitive data types

Primitive data types

The primitive data types are as follows:

String: The string data type represents a sequence of characters. It is written within quotes and can be represented using a single or a double quote.

Example:

var str1 = "Hello google"; //using double quotes  
var str2 = 'Hello google'; //using single quotes  

Number: The number data type is used to represent numeric values and can be written with or without decimals.

Example:

var x = 5; //without decimal  
var y = 5.0; //with decimal  

Boolean: The Boolean data type is used to represent a Boolean value, either false or true. This data type is generally used for conditional testing.

Example:

var x = 5;  
var y =  6;  
var z =  5;  
(x == y) // returns false  
(x == z) //returns true  

BigInt: The BigInt data type is used to store numbers beyond the Number data type limitation. This data type can store large integers and is represented by adding “n” to an integer literal.

Example:

var bigInteger =  123456789012345678901234567890;  
// This is an example of bigInteger.  

Undefined: The Undefined data type is used when a variable is declared but not assigned. The value of this data type is undefined, and its type is also undefined.

Example:

var x; // value of x is undefined  
var y = undefined; // You can also set the value of a variable as undefined.  

Null: The Null data type is used to represent a non-existent, null, or a invalid value i.e. no value at all.

Example:

var  x = null;  

Symbol: Symbol is a new data type introduced in the ES6 version of JavaScript. It is used to store an anonymous and unique value.

Example:

var symbol1 = Symbol('symbol');  

typeof: The typeof operator is used to determine what type of data a variable or operand contains. It can be used with or without parentheses (typeof(x) or typeof x). This is mainly used in situations when you need to process the values of different types.

Example:

typeof 10;  // Returns: "number"  
typeof 10.0;  // Returns: "number"  
typeof 2.5e-4;  // Returns: "number"  
typeof Infinity;  // Returns: "number"  
typeof NaN;  // Returns: "number". Despite being "Not-A-Number"  
// Strings  
typeof '';  // Returns: "string"  
typeof 'Welcome to JavaTpoint';  // Returns: "string"  
typeof '12';  // Returns: "string". Number within quotes is typeof string  
// Booleans  
typeof true;  // Returns: "boolean"  
typeof false;  // Returns: "boolean"  
// Undefined  
typeof undefined;  // Returns: "undefined"  
typeof undeclaredVariable; // Returns: "undefined"  
// Null  
typeof Null;  // Returns: "object"  
// Objects  
typeof {name: "John", age: 18};  // Returns: "object"  
// Arrays  
typeof [1, 2, 3];  // Returns: "object"  
// Functions  
typeof function(){};  // Returns: "function"  

Non-Primitive data types

In the above examples, we can see that the primitive data types can store only a single value. To store multiple and complex values, we have to use non-primitive data types.

The non-primitive data types are as follows:

Object: The Object is a non-primitive data type. It is used to store collections of data. An object contains properties, defined as a key-value pair. A property key (name) is always a string, but the value can be any data type, such as strings, numbers, Booleans, or complex data types like arrays, functions, and other objects.

Example:

// Collection of data in key-value pairs  
var obj1 = {  
   x:  123,  
   y:  "Welcome to JavaTpoint",  
   z: function(){  
      return this.x;  
   }  
}  

Array: The Array data type is used to represent a group of similar values. Every value in an array has a numeric position, called its index, and it may contain data of any data type-numbers, strings, Booleans, functions, objects, and even other arrays. The array index starts from 0 so that the first array element is arr[0], not arr[1].

Example:

var colors = ["Red", "Yellow", "Green", "Orange"];  
var cities = ["Noida", "Delhi", "Ghaziabad"];  
alert(colors[2]);   // Output: Green  
alert(cities[1]);   // Output: Delhi  

Leave a Reply

Your email address will not be published. Required fields are marked *