Datatypes In JavaScript

In this article, we learn about the datatypes in JavaScript. There are different types of datatypes in JavaScript. Let us see the types.
 

Definition

 
Datatype is a basic type of data that can be used in a program. JavaScript provides different data types to hold the different types of values. In JavaScript there is no need to specify the type of variable, because it is dynamically used by JavaScript Engine. The different types of datatypes in JavaScript are shown in the  flowchart below:
Datatypes In JavaScript
There are two major data types in JavaScript,
  • Primitive Data Types
  • Non-primitive Data Types/ composite data type

Primitive Data Types

  • Number - It represent numeric Value
  • String - is a sequence of Characters
  • Boolean - represent Boolean value either True or False
  • Undefined - it represents undefined value
  • Null - represents no value
Number
 
Number is representing the numeric value, number type represents integer, float, decimal. You need to use var here to specify the datatype. It can hold any type of value.
 
Example
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4.     <title>Java Script</title>    
  5. </head>    
  6. <body>    
  7.     <h4>JavaScript</h4>    
  8.     <p id="p1"></p>    
  9.     <p id="p2"></p>    
  10.     <p id="p3"></p>    
  11. <script>    
  12.     var a =100; //integer    
  13.     var b =10.5; //float    
  14.     var c =24.00; //decimal    
  15.     document.getElementById("p1").innerHTML = a;     
  16.     document.getElementById("p2").innerHTML = b;     
  17.     document.getElementById("p3").innerHTML = c;     
  18. </script>    
  19. </body>    
  20. </html>    
Output
 
Datatypes In JavaScript
 
String
 
String is a sequence of characters, the character must be representing single or double quotations.
 
E.g. “Hello World”, ‘Hello World’
 
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Java Script</title>  
  5. </head>  
  6. <body>  
  7.        <h4>JavaScript</h4>  
  8.     <script>  
  9.         var fname = "Vijay";  
  10.         var lastname= "Kumar";  
  11.             document.write(fname,lastname);  
  12.         </script>  
  13. </body>  
  14. </html>  
Output
 
Datatypes In JavaScript
 
Boolean
 
Boolean can only have two values, True or False. Boolean is used in conditional testing.
  1. var yes =” true”;  
  2. var no =” false”;  
Example
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Java Script</title>  
  5. </head>  
  6. <body>  
  7.     <h4>JavaScript</h4>  
  8.     <script>  
  9.     var user = (prompt("Enter the Number"))  
  10.     if (user < 10)  
  11.     {  
  12.     alert("True");  
  13.     document.write("Welcome to My Article");  
  14.     }  
  15.     else      
  16.     {  
  17.      alert("False");  
  18.      document.write("Access denide");  
  19.     }  
  20. </script>  
  21. </body>  
  22. </html>  
Output
 
Datatypes In JavaScript
 
Datatypes In JavaScript 
 
Undefined and Null datatype
 
Variable does not have any value but it will have later on.
  1. var nullvalue = null;  
  2. alert(nullvalue); // null  
  1. var nullvalue = null;  
  2. alert(nullvalue); // null  

Non-Primitive Data Type

  1. Object – Key Value pair
  2. Array – group of similar values

Summary

 
In this article, we looked at the data type in JavaScript. In my next article we will learn about objects in JavaScript. I hope this article is very useful for you. Thanks for reading.