logologo

Three-D

☕Sign in

    Recent Post:

    Categories:

    nextjsjavascriptthreejshonoreactjs
    featured post image

    snippets

    featured post image

    What is Currying?

    featured post image

    What is an IIFE?

    Understanding Data Types in JavaScript

    September 21, 2024

    760

    image

    Think you know it all?

    Take quiz!

    Explanation:

    In JavaScript, a "data type" is the kind of information you're working with. It's like how you have different types of things in real life—like a book, a toy, or a pencil. Each one is different and used in different ways.

    In JavaScript, there are Primitive Data Types (simple ones) and Non-Primitive Data Types (more complex ones).

    Primitive Data Types

    1. String: Think of this as words or text. You write it in quotes.

    code

    let name = "Satyendra";

    Here, "Satyendra" is a string.

    2. Number: These are just numbers, like how you count things.

    code

    let age = 25;

    25 is a number.

    3. BigInt: This is for super big numbers that normal numbers can't handle.

    code

    let bigNumber = 12345678901234567890n;

    The n at the end makes it a bigint.

    4. Boolean: This is either true or false, like a light switch that can be ON (true) or OFF (false).

    code

    let isStudent = true;

    true is a boolean.

    5. Undefined: This means you haven’t put anything in the box yet—it’s empty.

    code

    let job;

    job is undefined because we haven’t put anything in it yet.

    6. Null: This is an empty value, meaning "nothing is here."

    code

    let car = null;

    car is null because we’ve said there’s nothing in it.

    Code Example 1 (Primitive Data Types):

    code

    let name = "Alice";        // string
    let age = 10;              // number
    let isHappy = true;        // boolean
    let largeNumber = 123456789n;  // bigint
    let color;                 // undefined
    let favoriteFood = null;   // null

    Explanation for Code Example 1:

    We have six variables: name is a string holding the value "Alice." age is a number with the value 10. isHappy is a boolean with the value true, meaning yes. largeNumber is a really big number (bigint). color is undefined because we didn’t give it a value yet. favoriteFood is null, which means there’s nothing in that box for now.

    Non-Primitive Data Type

    Object: This is more complex—it can hold many different things inside, like a backpack with different items.

    code

    let person = {
        name: "Alice",
        age: 10,
        isStudent: true
    };

    Here, person is an object with three pieces of information: name, age, and isStudent.

    Code Example 2 (Non-Primitive Data Type - Object):

    code

    let car = {
        brand: "Toyota",
        model: "Corolla",
        year: 2022,
        isElectric: false
    };

    Explanation for Code Example 2:

    car is an object. Think of it like a backpack that holds many things: The brand is "Toyota." The model is "Corolla." The year is 2022. And isElectric is false, meaning the car is not electric.

    Objects are used when you want to group lots of information together in one place.