Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Explained var, let, const and Data types in Javascript

Updated
β€’4 min readβ€’View as Markdown
Understanding Variables and Data Types in JavaScript

A variable is a container used to store data. Think of it like a labeled box πŸ“¦ where you keep something.

let name = "Harshit";
  • name β†’ variable (label on the box)

  • "Harshit" β†’ value (what’s inside the box)

πŸ‘‰ Without variables, you can’t save values.
πŸ‘‰ You write it once, use it many times.
πŸ‘‰ Values can change β†’ programs become interactive.
πŸ‘‰ Variables make logic possible

  1. How to declare variables using var, let, and const
    To declare a variable in JavaScript, you type the keyword (var, let, or const), followed by the variable name, and optionally use the assignment operator (=) to give it a value.

    Syntax:
    var variableName = value;
    let variableName = value;
    const variableName = value;
    
    • Using var

      var name = "John";
      

      πŸ‘‰ Can be redeclared
      πŸ‘‰ Can be updated
      πŸ‘‰ Function scoped

      var x = 10;
      var x = 20; // allowed
      x = 30;     // allowed
      
    • Using let

      let age = 25;
      

      πŸ‘‰ Cannot be redeclared in the same scope
      πŸ‘‰ Can be updated
      πŸ‘‰ Block scoped

      let age = 25;
      // let age = 30; error
      age = 30;     // allowed
      
    • Using const

      const pi = 3.14;
      // pi = 3.15; error
      

      πŸ‘‰ Cannot be redeclared
      πŸ‘‰ Cannot be updated
      πŸ‘‰ Block scoped
      πŸ‘‰ Must be initialized immediately
      πŸ‘‰ const prevents reassignment, not mutation

      const person = { name: "John" };
      person.name = "Alex"; // allowed
      
      const crewRoster = ["Alok", "Abhinav", "Tasnish"];
      crewRoster[0] = "Shubham";
      // crewRoster = ["Someone"]; cann't do this
      
Feature var 🟑 let πŸ”΅ const 🟒
Scope Function scope Block scope {} Block scope {}
Hoisting Yes (initialized as undefined) Yes (but in TDZ*) Yes (but in TDZ*)
Re-declaration Allowed Not allowed Not allowed
Re-assignment Allowed Allowed Not allowed
Initial Value Optional Optional Required
Temporal Dead Zone (TDZ) No Yes Yes
Global Object Binding Yes (window.varName) No No
Use Case Legacy code (avoid) Variables that change Constants / fixed references

Seven data types that are primitives (Basic / Immutable)

πŸ‘‰ Primitive = Single value

  1. Boolean: true and false.

    let isLoggedIn = true;
    
  2. null: A special keyword denoting a null value. (Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.)

    let y = null;
    
  3. undefined: A top-level property whose value is not defined.

    let x; // x = undefined;
    
  4. Number: An integer or floating point number. For example: 42 or 3.14159.

    let age = 25;
    
  5. BigInt: An integer with arbitrary precision. For example: 9007199254740992n.

    let big = 12345678901234567890n;
    
  6. String: A sequence of characters that represents a text value. For example: "Howdy".

    let name = "John";
    
  7. Symbol: A data type whose instances are unique and immutable.

    let id = Symbol("unique");
    

Non-Primitive Data Types (Reference Types)

πŸ‘‰ These store collections or complex data
πŸ‘‰ Stored by reference (memory address)

  1. Object

    let user = { name: "John", age: 25 };
    
  2. Array

    let numbers = [1, 2, 3];
    
  3. Function

    function greet() {
      console.log("Hello");
    }
    
console.log(typeof "chaicode");   // string
console.log(typeof 42);           // number
console.log(typeof 42n);          // bigint
console.log(typeof true);         // boolean
console.log(typeof undefined);    // undefined
console.log(typeof null);         // object ⚠️
console.log(typeof Symbol());     // symbol
console.log(typeof {});           // object
console.log(typeof []);           // object ⚠️
console.log(typeof function () {}); // function

Types of Scope in JavaScript

  1. Global Scope
    πŸ‘‰ Variable declared outside everything
    πŸ‘‰ Can be used anywhere

    let name = "John";
    
    function greet() {
      console.log(name); // βœ… accessible
    }
    
  2. Local Scope (Function Scope)
    πŸ‘‰ Variable declared inside a function
    πŸ‘‰ Only usable inside the function

    function greet() {
      let message = "Hello";
      console.log(message); // accessible
    }
    
    // console.log(message); Error
    
  3. Block Scope (let & const)
    πŸ‘‰ Inside { } like if, for
    πŸ‘‰ Only inside that block

    if (true) {
      let age = 25;
      console.log(age); // accessible
    }
    
    // console.log(age); Error
    
  4. Special Case: var
    πŸ‘‰ var ignores block scope (this is why it's avoided)

    if (true) {
      var x = 10;
    }
    
    console.log(x); // works
    
6 views