JavaScript Data Type| Number | Boolean

In this, you will learn about the various data types available in JavaScript with the help of examples.
There are different types of data that we can use in a JavaScript program. For example:

const x = 5;

Definition

The data type is a basic type of data that can be used in a program. JavaScript provides different data types to hold 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 data types in JavaScript are shown in the flowchart below

There are two major data types in JavaScript;

  • Primitive Data Types
  • Non-primitive Data Types/ composite data type

Primitive Data Types Number

  • 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 is representing the numeric value, the number type represents integer, float, decimal. You need to use here to specify the datatype. It can hold any type of value

.

<!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:

100 //Integer
10.5 //Float
24 //Decimal

String

The string is a sequence of characters, the character must be representing single or double
quotations.
E.g. “Hello World”, „Hello World‟

<!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

JavaScript
VijayKumar //String

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

Undefined and Null datatype

Variable does not have any value but it will have later on.

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

Non-Primitive Data Type

  • Object– Key Value pair
  • Array– group of similar values
const y = "Hello";

Here:

  • 5 is an integer data.
  • “Hello” is a string data.

Summary

Data TypesDescriptionExample
Stringrepresents textual data‘hello’, “hello world!” etc
Numberan integer or a floating-point number3, 3.234, 3e-2 etc
BigIntan integer with arbitrary precision900719925124740999n , 1n etc.
BooleanAny of two values: true or falsetrue and false
undefineda data type whose variable is not initializedlet a;
nulldenotes a null valuelet a = null;
Symboldata type whose instances are unique and immutablelet value = Symbol(‘hello’);
Objectkey-value pairs of collection of datalet student = { };

Here, all data types except Object are primitive data types, whereas Object is non-primitive.

JavaScript Data Type| Number | Boolean
Show Buttons
Hide Buttons