Tuesday, May 14, 2024

JavaScript DataTypes

JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.

1)    Primitive data type
2)    Non-primitive (reference) data type


JavaScript is a dynamic type language, means you don't need to specify type of the variable because it is dynamically used by JavaScript engine. You need to use var here to specify the data type. It can hold any type of values such as numbers, strings etc. For example:

1)    var a=40;//holding number  
2)    var b="Rahul";//holding string  

JavaScript primitive data types

There are five types of primitive data types in JavaScript. They are as follows:

Data Type        Description
String            represents sequence of characters e.g. "hello"
Number            represents numeric values e.g. 100
Boolean        represents boolean value either false or true
Undefined        represents undefined value
Null            represents null i.e. no value at all

JavaScript non-primitive data types

The non-primitive data types are as follows:

Data Type        Description
Object            represents instance through which we can access members
Array            represents group of similar values
RegExp            represents regular expression


<!DOCTYPE>
<html>
<head>
    <title>JavaScript</title>
    <script>
        /* String Data Type */
        var a = "Hello World";
        document.write(a);
        document.write("<br>");
        document.write(typeof a);
        document.write("<br><br>");

        /* Number Data Type */
        var b = 25;
        document.write(b);
        document.write("<br>");
        document.write(typeof b);
        document.write("<br><br>");

        /* Boolean Data Type */
        var c = true;
        document.write(c);
        document.write("<br>");
        document.write(typeof c);
        document.write("<br><br>");

        /* Array Data Type */
        var d= ["HTML","CSS","JS"];
        document.write(d);
        document.write("<br>");
        document.write(typeof d);
        document.write("<br><br>");

        /* Object Data Type */
        var x= {first:"Jane",last:"Doe"};
        document.write(x);
        document.write("<br>");
        document.write(typeof x);
        document.write("<br><br>");

        /* Null Data Type */
        var y = null;
        document.write(y);
        document.write("<br>");
        document.write(typeof y);
        document.write("<br><br>");

        /* undefined Data Type */
        var z;
        document.write(z);
        document.write("<br>");
        document.write(typeof z);
        document.write("<br><br>");
    </script>
</head>
<body>
</body>
</html>

No comments:

Post a Comment