Datatypes In JavaScript

Introduction

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

Definition

The data type is a primary data type that can be used in a program. JavaScript provides different data types to hold different types of values. There is no need to specify the variable type because the JavaScript Engine dynamically uses it. The different types of data types in JavaScript are shown in the  flowchart below,

Datatypes In JavaScript

There are two significant data types in JavaScript,

  • Primitive Data Types

  • Non-primitive Data Types/ composite data type

Primitive Data Types

  • Number- It represents numeric Value

  • String- is a sequence of Characters

  • Boolean- represent Boolean value either True or False

  • Undefined- it represents an undefined value

  • Null- represents no value

Number

The number represents the numeric value; the number type represents integer, float, or decimal. You need to use var here to specify the datatype. It can hold any type of value.

Example

<!DOCTYPE html>    
<html>    
<head>    
    <title>Java Script</title>    
</head>    
<body>    
    <h4>JavaScript</h4>    
    <p id="p1"></p>    
    <p id="p2"></p>    
    <p id="p3"></p>    
<script>    
    var a =100; //integer    
    var b =10.5; //float    
    var c =24.00; //decimal    
    document.getElementById("p1").innerHTML = a;     
    document.getElementById("p2").innerHTML = b;     
    document.getElementById("p3").innerHTML = c;     
</script>    
</body>    
</html>    

Output

Datatypes In JavaScript

String

The string is a sequence of characters; the character must represent single or double quotations.

E.g., “Hello World”,”‘Hello World’

Example

<!DOCTYPE html>  
<html>  
<head>  
    <title>Java Script</title>  
</head>  
<body>  
       <h4>JavaScript</h4>  
    <script>  
        var fname = "Vijay";  
        var lastname= "Kumar";  
            document.write(fname,lastname);  
        </script>  
</body>  
</html>  

Output

Datatypes In JavaScript

Boolean

Boolean can only have two values, True or False. Boolean is used in conditional testing.

var yes =” true”;  
var no =” false”;  

Example

<!DOCTYPE html>  
<html>  
<head>  
    <title>Java Script</title>  
</head>  
<body>  
    <h4>JavaScript</h4>  
    <script>  
    var user = (prompt("Enter the Number"))  
    if (user < 10)  
    {  
    alert("True");  
    document.write("Welcome to My Article");  
    }  
    else      
    {  
     alert("False");  
     document.write("Access denide");  
    }  
</script>  
</body>  
</html>  

Output

Datatypes In JavaScript

Datatypes In JavaScript

Undefined and Null datatype

The variable has no value, but it will have later on.

var nullvalue = null;  
alert(nullvalue); // null  
var nullvalue = null;  
alert(nullvalue); // null  

Non-Primitive Data Type

  1. Object- Key-Value pair
  2. Array- a 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 beneficial for you. Thanks for reading.