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:

  1. var str1 = “Hello JavaTpoint”; //using double quotes  
  2. var str2 = ‘Hello Javatpoint’; //using single quotes  

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

Example:

  1. var x = 5; //without decimal  
  2. 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:

  1. var x = 5;  
  2. var y =  6;  
  3. var z =  5;  
  4. (x == y) // returns false  
  5. (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:

  1. var bigInteger =  123456789012345678901234567890;  
  2. // 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:

  1. var x; // value of x is undefined  
  2. 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:

  1. 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:

  1. 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:

  1. typeof 10;  // Returns: “number”  
  2. typeof 10.0;  // Returns: “number”  
  3. typeof 2.5e-4;  // Returns: “number”  
  4. typeof Infinity;  // Returns: “number”  
  5. typeof NaN;  // Returns: “number”. Despite being “Not-A-Number”  
  6. // Strings  
  7. typeof ”;  // Returns: “string”  
  8. typeof ‘Welcome to JavaTpoint’;  // Returns: “string”  
  9. typeof ’12’;  // Returns: “string”. Number within quotes is typeof string  
  10. // Booleans  
  11. typeof true;  // Returns: “boolean”  
  12. typeof false;  // Returns: “boolean”  
  13. // Undefined  
  14. typeof undefined;  // Returns: “undefined”  
  15. typeof undeclaredVariable; // Returns: “undefined”  
  16. // Null  
  17. typeof Null;  // Returns: “object”  
  18. // Objects  
  19. typeof {name: “John”, age: 18};  // Returns: “object”  
  20. // Arrays  
  21. typeof [1, 2, 3];  // Returns: “object”  
  22. // Functions  
  23. 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:

  1. // Collection of data in key-value pairs  
  2. var obj1 = {  
  3.    x:  123,  
  4.    y:  “Welcome to JavaTpoint”,  
  5.    z: function(){  
  6.       return this.x;  
  7.    }  
  8. }  

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:

  1. var colors = [“Red”, “Yellow”, “Green”, “Orange”];  
  2. var cities = [“Noida”, “Delhi”, “Ghaziabad”];  
  3. alert(colors[2]);   // Output: Green  
  4. alert(cities[1]);   // Output: Delhi