JavaScript - Datatypes & Hoisting

Data Types :

JavaScript is a dynamically typed scripting language. In JavaScript, variables can receive different data types over time. The latest ECMAScript standard defines eight data types Out of which seven data types are Primitive (predefined) and one complex or Non-Primitive.

i. Primitive Data Types :
The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.
  1. Number : JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754. Unlike other programming languages, you don’t need int, float, etc to declare different numeric values.
  2. String : JavaScript Strings are similar to sentences. They are made up of a list of characters, which is essentially just an “array of characters, like “Welcome to Tesdbacademy” etc.
  3. Boolean : Represent a logical entity and can have two values: true or false.
  4. Null : This type has only one value that is null.
  5. Undefined : A variable that has not been assigned a value is undefined.
  6. Symbol : Symbols return unique identifiers that can be used to add unique property keys to an object that won’t collide with keys of any other code that might add to the object.
  7. BigInt : BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 2^53-1.
ii. Non-Primitive Data Types
The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.
  • Object : It is the most important data type and forms the building blocks for modern JavaScript. We will learn about these data types in detail in further articles. Examples of Primitive and Non-primitive datatypes:
        // Numbers:
        let length = 16;
        let weight = 7.5;
    
        // Strings:
        let color = "Yellow";
    
        let lastName = "Johnson";
    
        // Booleans
        let x = true;
        let y = false;
    
        // Object:
        const person = {firstName:"John", lastName:"Doe"};
    
        // Array object:
        const cars = ["Mahindra", "Tata", "BMW"];
    
        // Date object:
        const date = new Date("2024-08-31");
    
    Main example for Primitive and Non-primitive datatypes:
        < !DOCTYPE html>
        < html>
        < body>
        < h2>JavaScript Objects< /h2>
        < p id="demo">< /p>
        < hr>
        < h1>JavaScript Operators< /h1>
        < h2>The typeof Operator< /h2>
        < p>The typeof operator returns the type of a variable or an expression.< /p>
    
        < script>
        const person = {
            firstName : "Java",
            lastName  : "Script",
            age     : 39,
            eyeColor  : "blue"
        };
    
        document.getElementById("demo").innerHTML = person.firstName + " is " + person.age + " years old.";
    
        document.getElementById("demo1").innerHTML = typeof 0 + "< br>" + 
                                                     typeof 314 + "< br>" +
                                                     typeof 3.14 + "< br>" +
                                                     typeof (3) + "< br>" +
                                                     typeof (3 + 4);
        < /script>
        < /body>
        < /html>
    
    Output :
    JavaScript Objects
    Java is 39 years old.
    ________________________________________
    JavaScript Operators
    The typeof Operator
    The typeof operator returns the type of a variable or an expression.


    Hoisting :

    Javascript allows a programmer to a member(variable) before the declaration state this characteristics is known as hoisting. JavaScript Declarations are Hoisted.In JavaScript, a variable can be declared after it has been used. In other words, a variable can be used before it has been declared.
    Example 1 gives the same result as Example 2:
        Example 1:
        x = 5; // Assign 5 to x
    
        elem = document.getElementById("demo"); // Find an element
        elem.innerHTML = x;                     // Display x in the element
    
        var x; // Declare x
    
        Example 2:
        var x; // Declare x
        x = 5; // Assign 5 to x
    
        elem = document.getElementById("demo"); // Find an element
        elem.innerHTML = x;                     // Display x in the element
    
        Eg:
        console.log(x);    //undefined
        var x= “Javascript”;
        console.log(x);	//Javascript
    
    


    (JS - Operators)