JavaScript Data Types and Data Structures

    Every Programming Language has its built-in data types and data structures. Although most of the Programming Languages share the same concepts for Data Types and Data Structure, there are some which support unique Data Types and Data Structure. Here in this JavaScript tutorial, you will learn all the built-in Data Types and Structures present in JavaScript.

    Data Types in JavaScript

    When we declare or initialize a variable in JavaScript, we do not define its data type because JavaScript is a Dynamically types programming language. If you have ever worked with static-typed programming languages like C++, C and Java there we need to define the variable datatype when we declare a variable. In Static-Typed C++ Language

    int age =  23;

    In Dynamic-Typed JavaScritp Language

    age = 23;

    In the above code snippet, you can see that in C++ we defined int (integer) data type to the age variable but in JavaScript, we did not need to define the variable type, because JavaScript is a Dynamic-Typed language, and the JavaScript engine handles the type specification for us. So what is a Data Type? A Data Type defines the type of value that a variable is holding. For example, if a variable is holding a number value then its Data type will be number in JavaScript, if it is holding a text value then its type will be the string .

    Types of Data Types in JavaScript

    According to the Latest ECMAScript Standard, the JavaScript Data Types are divided into 2 categories.

    1. JavaScript Primitive Data Types
    2. JavaScript Non-Primitive or Structural Data Types.

    JavaScript Primitive Data Types

    There are 7 built-in Primitive Data Types present in JavaScript

    1. Number
    2. String
    3. Boolean
    4. undefined
    5. Null
    6. Bigint (New)
    7. Symbol (New)

    1. Number If a variable is holding a decimal or non-decimal number value such as integer, real-number, whole number, or natural number, then the data type of the variable would be number. Example

    let num1 = 1223;
    let num2 = 122.2323;
    let num3 = -2323.33;
    let num4 = 0;

    2. String A string is a text value that can be contained inside the pair of single quotes ' string' , double quotes "string" or backticks `string` . Example

    //with single quote
    let string1 = 'one*()101'
    
    // with double quote
    let string2 = "two 222@@"
    
    //with backticks
    let string3 = `three 3333@@@`

    3. Boolean The Boolean data type defines true and false in JavaScript, and it has only two value true and false . Example

    let adult = true;
    let adult = false;

    4. Undefined The undefined is not a value it is a state or data type assigned to a variable when it is only declared not initialized. Example

    //here a is undefined
    let a;

    5. Null The null is a special data type value, and it is used when you want to assign no value to the variable rather than leaving it undefined. The data type of null value is object which is a structural Data type.

    // null value
    let email = null
    
    

    6. Bigint Bigint stands for big integer and is the new data type introduced to JavaScript in the latest ECMAScript standards. By default the maximum number the JavaScript can deal with is 2 53 which is 9007199254740992 , but if we wish to use a number greater than 2 53 then we can use the Bigint data type in JavaScript. To initialize a Bigint number in JavaScript we can either use the Bigint() object or place n at the end of the number. Example

    let bignum = BigInt(23); 
    
    // put n at last 
    let bignum2 = 23n;

    7. Symbol Symbol is also a new data type introduced to JavaScript in the latest ECMASccript standards. To define a Symbol data type in JavaScipt we can use Symbol() object. The Symbol data type is used to create unique and immutable values, two symbols data type values can not be equal. Example

    let sym1 = Symbol(1); 
    let sym2 = Symbol("two");

    JavaScript Non-Primitive Data Types or Data Structure

    There are two major and widely used Non-Primitive Data Types, which are also known as JavaScript built-in Data Structure.

    1. Objects
    2. Arrays

    As the Name Suggest the Data Structure is used to structure the data value, and it generally structures the primitive data value or data types. 1. Objects An object in JavaScript is a collection of name:value pairs, where the name could be a variable name and the value is the data value. All the name:value pairs in the JavaScript object can be defined inside the curly brackets {} separated by commas. Example

    let obj1= {name:"Rahul", age:21}
    
    let obj2 = {name:"Ravi", age:23}

    2. Array An array in JavaScript is a sequential collection of data values. To declare an array in JavaScript we use the square brackets.

    let arr = [1, 'one', 2.0, true]
    

    How to Get the Data Type of Variable in JavaScript?

    JavaScript provides a built-in typeof keyword which returns the data type of the variable. We can use typeof operator and get the data type of the variable. Example

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
            <script>
                document.write("<h1> JavaScript Data Types</h1>");
    
                //primitive data types
                let num = 23;
                let str = "Hello23"
                let boolean = true;
                let undef;
                let noval = null;
                let bignum = 23n;
                let symbol = Symbol("1");
    
                //non-primitive data types
                let obj ={name:"value1", name2:"value2"};
                let arr = [1, 2, 3,4];
    
                document.write('The Data Type of num is: '+typeof num);
                document.write("<br>")
    
                document.write('The Data Type of str is: '+typeof str);
                document.write("<br>")
    
                document.write('The Data Type of boolean is: '+typeof boolean);
                document.write("<br>")
    
                document.write('The Data Type of undef is: '+typeof undef);
                document.write("<br>")
    
                document.write('The Data Type of noval is: '+typeof noval);
                document.write("<br>")
    
                document.write('The Data Type of bignum is: '+typeof bignum);
                document.write("<br>")
    
                document.write('The Data Type of symbol is: '+typeof symbol);
                document.write("<br>")
    
                document.write('The Data Type of obj is: '+typeof obj);
                document.write("<br>")
    
                document.write('The Data Type of arr is: '+typeof arr);
                document.write("<br>")
            </script>     
        <body>
        </body>
    </html>

    Summary

    • Data Type defines the type of the data value.
    • JavaScript Data Types is divided into two categories primitive and non-primitive data types.
    • There are 7 primitive data types in JavaScript number, string, undefined, null, boolean, Symbol, and Bigint.
    • The non-primitive data types are also known as the Data Structure or JavaScript structural data types.
    • There are two major structural data types in JavaScript objects and Arrays.
    • To find the data type of a variable we can use the typeof operator.

    People are also reading: