●   There is No need to mensioned Datatype on variable declaration, JS auto detect it.
●   It is important to understand types of Datatapes and its values.

   Primitive DataType
   Non Primitive DataType


◈ Primitive Data Types

Number

Used for integers and decimals.


        let age = 25;
        let price = 99.99;
        let score = NaN;      // Not a Number (still type "number")
        let inf = Infinity;   // Represents infinite number
    

        const pi = 22/7 ;
        let radius = prompt("Enter radius of Circle in cm."); // Take Input by Prompt Box
        let areaOfCircle = pi * (radius * radius);   // Store Formulas
        console.log( areaOfCircle + ' cm');
    
Output in console,

        // Check Console by F12 or Right Click and check Inspect and then Console

        00.000 cm // follows this formate for output OR
        NaN       // If input is not the number
    

String

Text is written using 'single', "double", or `backticks`.


        let name = "Alice";
        let greeting = 'Hello!';
        let template = `Hi, ${name}`; // Template string using backticks
        console.log(template);
    
Output in console,

        Hi, Alice
    

Boolean

Only two values: true or false

true contain 1,
false contain 0.


        let isLoggedIn = true;
        let hasPermission = false;

        console.log(isLoggedIn);    // true
        console.log(hasPermission); // false

        console.log(isLoggedIn + isLoggedIn);        // 1 + 1 = 2
        console.log(isLoggedIn + hasPermission);     // 1 + 0 = 1
        console.log(hasPermission + hasPermission);  // 0 + 0 = 0
    
In console,

        true
        false
        2
        1
        0
    

Undefined

Means a variable is declared but not yet assigned a value.


        let Yellowfruit;
        Yellowfruit = "Banana"
        console.log(Yellowfruit); // Banana is defined

        let Redfruit ;
        // Redfruit = "Apple"   // Commented | Apple not gonna assign
        console.log(Redfruit); // undefined

        let Greenfruit ;
        console.log(Greenfruit); // undefined
    
In console,

        Banana     // Banana is assigned
        undefined  // Apple is commented
        undefined  // only Declared
    

Null

Means intentionally empty or “no value”.

In undefined there is no actuall value to assign.
but null is different, It actuall value which say its NULL.


        let person = null;
    

BigInt

Used for very large numbers beyond normal Number limits.


        let big = 123456789012345678901234567890n;
    

Symbol

A Symbol is a primitive data type introduced in ES6 (ECMAScript 2015).

It represents a unique and immutable value.
Symbols are mostly used to create unique keys for object properties — without the risk of name collision.


        const userId = Symbol('userId'); // Create Symbol

        let user = {
          name: "Alice",
          [userId]: 101 
        };

        console.log(user[userId]); // 101
    

        101
    

◈ Non-Primitive (Reference) Data Types

Not Primitives are more complex and can store multiple values.

Object

Collection of Key Value pair.


        // Object Student
        let Student = {
            name: "John",
            marks: 30,
            course: "IT"
        };

        console.log(Student.name); // "John"
        console.log(Student["marks"]); // 30

        // Access all properties at once
        for (let key in Student) {
          console.log(`${key}: ${Student[key]}`);
        }
    

        john  // Accessing by Dot Notation
        30    // Accessing by Braces Notation

        name: John
        marks: 30
        couse: IT
    

Arrays

An ordered list of values (like a list or group).


        // Array of fruits
        let fruits = ["apple", "banana", "mango"];

        console.log(fruits[0]); // "apple"
        console.log(fruits[2]); // "mango"

        // Access all values at once
        for (let fruit in fruits) {
          console.log(fruits[fruit]);
        }
    

        apple
        mango

        // Access all together
        apple
        banana
        mango
    

Function

A block of code you can reuse. It's also a special kind of object. Can use for perform task as required.


        function greet() {
          console.log("Hello!");
        }

        greet();
    

        hello!
    

◈ Type Checking

Used to check the data type of any value.


        console.log(typeof 42);       // "number"
        console.log(typeof "hello");  // "string"
        console.log(typeof null);     // "object" (weird, but historical bug)
    

        let numeric = 10 ;
        let stringy = "heyy buddy";
        let bulean = true;
        let nodata ;
        let novalue = Null;

        console.log("numeric is " + typeof (numeric));
        console.log("stringy is " + typeof (stringy));
        console.log("bulean is " + typeof (bulean));
        console.log("nodata is " + typeof (nodata));
        console.log("novalue is " + typeof (novalue));

        alert("numeric is " + typeof (numeric) + "\n" +
              "stringy is " + typeof (stringy) + "\n" +
              "bulean is " + typeof (bulean) + "\n" +
              "nodata is " + typeof (nodata) + "\n" +
              "novalue is " + typeof (novalue))

    
Output in console,

        numeric is number
        stringy is string
        bulean is boolean
        nodata is undefined
        novalue is object
    

◈ Type Conversion in JavaScript

There are two types type Conversion exist in javasript.

Implicit (Auto Conversion by JS)
Explicit (Manually Convert)

Implicit

JavaScript automatically converts types in expressions.


        console.log("5" + 2);   // "52"  → number 2 is converted to string
        console.log("5" - 2);   // 3     → string "5" is converted to number
        console.log(true + 1);  // 2     → true becomes 1
    

        52
        3
        2
    

Explicit

Use functions: String(), Number(), Boolean()


        String(100);
        '100'

        Number("42");
        42

        Boolean(0);
        false

        parseInt("10px");
        10

        parseFloat("3.14");
        3.14
    

◈ Equality: == vs ===

== (Loose Equality)

Compares values, but converts types if needed.


        console.log("5" == 5 );        // true (string is converted to number)
        console.log(false == 0);       // true
        console.log(null == undefined);// true
    

        true
        true
        true
    

=== (Strict Equality)

Compares values and types — no conversion.


        console.log("5" === 5);          // false (string vs number)
        console.log(false === 0);        // false
        console.log(null === undefined); // false
    

        false
        false
        false
    

◈ Mutability

Mutable = can be changed
Objects and Arrays are mutable


        const client = { name: "Alice" };
        console.log(client.name); // Alice

        client.name = "Bob"; // changed
        console.log(client.name); // Bob
    

        Alice
        Bob
    

Immutable = cannot be changed
Primitive values (string, number, boolean, null, undefined, symbol, bigint)


        let str = "hello";
        console.log(str);   // "hello"

        str[0] = "H";       // does nothing
        console.log(str);   // "hello"
    

        hello
        hello
    

◈ Value vs Reference

Value (Primitive Types)
Copied by Value


        let a = 10;
        let b = a;
        b = 20;
        console.log(a); // 10
    

        10
    

Reference (Objects, Arrays)
Copied by reference (both point to same object)


        let obj1 = { x: 1 };
        let obj2 = obj1;
        obj2.x = 5;
        console.log(obj1.x); // 5 (also changed)
    

        5
    

© JurneyToJs by Urvish Patel.