JavaScript Data Types

  • This lesson explains all JavaScript data types with practical examples.
  • What Are Data Types in JavaScript ?

    A data type defines:

    • What kind of data a variable can store

    • How much memory is needed

    • What operations can be performed on that data

    In simple words:

    Data types tell JavaScript what type of value is stored in a variable.

    Why Are Data Types Important ?

    Data types are important because:

    • JavaScript behaves differently for different types of data

    • Operations depend on data type

    • Memory management becomes efficient

    • Helps avoid logical errors

    Example:

    let age = 25;        // number

    let name = "Rahul";  // string

    JavaScript treats both variables differently.

    JavaScript is a Dynamically Typed Language

    In JavaScript:

    • You do not need to specify data types

    • Data type is decided at runtime

    Example:

    let x = 10;      // number

    x = "Hello";    // string

    x = true;       // boolean

    This is called dynamic typing.

    Classification of Data Types in JavaScript

    JavaScript data types are broadly divided into two categories:

    1. Primitive Data Types

    2. Non-Primitive (Reference) Data Types

    Primitive Data Types

    Primitive data types:

    • Store single values

    • Are immutable (cannot be changed)

    • Stored directly in memory

    • Copied by value

    JavaScript has 7 primitive data types.

    List of Primitive Data Types

    Data TypeDescription
    NumberNumeric values
    StringText values
    Booleantrue / false
    UndefinedDeclared but not assigned
    NullEmpty or intentional absence
    BigIntVery large integers
    SymbolUnique identifiers

    Number Data Type

    The Number type is used for:

    • Integers

    • Decimals

    • Positive and negative numbers

    Example:

    let age = 25;

    let price = 99.99;

    let temperature = -10;

    JavaScript does not differentiate between int and float.

    Special Number Values

    let x = Infinity;

    let y = -Infinity;

    let z = NaN; // Not a Number

    Example:

    console.log(10 / "a"); // NaN

    String Data Type

    A String is a sequence of characters.

    Strings can be written using:

    • Single quotes

    • Double quotes

    • Backticks (template literals)

      Example:

      let name = "Amit";

      let city = 'Delhi';

      let message = `Welcome to JavaScript`;

      String Example

      let firstName = "Rahul";

      let lastName = "Sharma";

      console.log(firstName + " " + lastName);

      Output:

      Rahul Sharma

      Boolean Data Type

      Boolean represents logical values:

      • true

      • false

      Example:

      let isLoggedIn = true;

      let isAdmin = false;

      Used heavily in:

      • Conditions

      • Loops

      • Decision making

      Undefined Data Type

      A variable that is declared but not assigned a value is undefined.

      Example:

      let x;

      console.log(x); // undefined

      JavaScript automatically assigns undefined.

      Null Data Type

      null represents:

      • Empty value

      • Intentional absence of data

      Example:

      let user = null;

      Difference:

      • undefined → value not assigned

      • null → value intentionally empty

      Important Note (Interview Point)

      typeof null; // "object"

      This is a known JavaScript bug, kept for backward compatibility.

      BigInt Data Type

      BigInt is used to store very large integers beyond Number limit.

      Example:

      let bigNumber = 123456789012345678901234567890n;

      Note:

      • BigInt values end with n

      • Cannot mix BigInt with Number directly

      Symbol Data Type

      Symbol creates unique identifiers.

      Example:

      let id1 = Symbol("id");

      let id2 = Symbol("id");

      console.log(id1 === id2); // false

      Used mainly in:

      • Advanced JavaScript

      • Object properties

      • Frameworks and libraries

    • Summary of Primitive Data Types
      FeaturePrimitive
      StoresSingle value
      MutabilityImmutable
      MemoryDirect
      CopyBy value

      Non-Primitive (Reference) Data Types

      Non-primitive data types:

      • Can store multiple values

      • Are mutable

      • Stored as references

      • Copied by reference

    • Main Non-Primitive Data Types
      TypeDescription
      ObjectKey-value pairs
      ArrayOrdered collection
      FunctionBlock of reusable code

    Data Types

    Demonstrates object, array, and function data types with basic usage.

    // Object Data Type (stores data in key:value pairs)
    let student = {
      name: "Ravi",     // name property
      age: 21,          // age property
      course: "JavaScript" // course property
    };
    
    // Accessing object property
    console.log(student.name); // Output: Ravi
    
    
    // Array Data Type (stores multiple values)
    let colors = ["red", "green", "blue"];
    
    // Accessing array value using index
    console.log(colors[0]); // Output: red
    
    
    // Function Data Type (reusable block of code)
    function greet() {
      console.log("Hello JavaScript"); // prints message
    }
    
    // Calling the function
    greet();
    • In JavaScript, functions are treated as objects.

      Primitive vs Non-Primitive

      FeaturePrimitiveNon-Primitive
      ValuesSingleMultiple
      StorageDirectReference
      MutableNoYes
      Examplenumber, stringobject, array
    • Copy by Value vs Copy by Reference

      Primitive Example

      let a = 10;

      let b = a;

      b = 20;

      console.log(a); // 10

      Original value is not affected.

      Non-Primitive Example

      let obj1 = { name: "Amit" };

      let obj2 = obj1;

      obj2.name = "Rahul";

      console.log(obj1.name); // Rahul

      Both variables point to the same memory location.

      typeof Operator

      Used to check data type.

    typeof Operator

    Demonstrates how to check the data type of different values using typeof.

    // Checking data types using typeof
    
    console.log(typeof 10);           // number
    console.log(typeof "Hello");      // string
    console.log(typeof true);         // boolean
    console.log(typeof {});           // object (object literal)
    console.log(typeof []);           // object (array is also an object)
    console.log(typeof function(){}); // function
    • Common Mistakes

      • Confusing null and undefined

      • Assuming arrays are not objects

      • Forgetting reference behavior

      • Using == instead of understanding data types

      Best Practices

      • Use meaningful variable names

      • Understand reference behavior clearly

      • Use const where possible

      • Avoid unnecessary type conversions