JavaScript Operators

  • This lesson covers different types of operators used in JavaScript.
  • Introduction to Operators in JavaScript

    In JavaScript, operators are symbols that are used to perform operations on values (operands).

    In simple words:

    Operators tell JavaScript what action to perform on data.

    Why Are Operators Important ?

    Operators are used everywhere in JavaScript:

    • Calculations

    • Conditions

    • Decision making

    • Logical checks

    • Comparisons

    Without operators:

    • No calculations

    • No conditions

    • No logic

    • No real programs

    Basic Structure of an Operator Expression

    operand operator operand

    Example:

    10 + 5

    • 10 → operand

    • + → operator

    • 5 → operand

      Types of Operators Covered in This Lesson

      1. Arithmetic Operators

      2. Comparison Operators

      3. Logical Operators

      Arithmetic Operators

      What Are Arithmetic Operators ?

      Arithmetic operators are used to perform mathematical calculations.

      They work with numbers.

      List of Arithmetic Operators

      OperatorNameDescription
      +AdditionAdds values
      -SubtractionSubtracts values
      *MultiplicationMultiplies values
      /DivisionDivides values
      %ModulusReturns remainder
      ++IncrementIncreases value by 1
      --DecrementDecreases value by 1

    Arithmetic and Increment Operators

    Demonstrates basic arithmetic operations and increment/decrement operators

    // Addition Operator (+)
    let a = 10;
    let b = 5;
    let result = a + b;
    console.log(result); // 15
    
    
    // Subtraction Operator (-)
    let a1 = 10;
    let b1 = 4;
    console.log(a1 - b1); // 6
    
    
    // Multiplication Operator (*)
    let a2 = 5;
    let b2 = 3;
    console.log(a2 * b2); // 15
    
    
    // Division Operator (/)
    let a3 = 20;
    let b3 = 4;
    console.log(a3 / b3); // 5
    
    
    // Modulus Operator (%) - returns remainder
    let a4 = 10;
    let b4 = 3;
    console.log(a4 % b4); // 1
    
    
    // Post-Increment (x++)
    let x = 5;
    console.log(x++); // 5 (first prints, then increases)
    console.log(x);   // 6
    
    
    // Pre-Increment (++y)
    let y = 5;
    console.log(++y); // 6 (first increases, then prints)
    
    
    // Decrement Operator (--)
    let x1 = 5;
    x1--;
    console.log(x1); // 4
    • Comparison Operators

      What Are Comparison Operators ?

      Comparison operators are used to compare two values.

      They always return a boolean value:

      • true

      • false

        List of Comparison Operators

        OperatorName
        ==Equal to
        ===Strict equal
        !=Not equal
        !==Strict not equal
        >Greater than
        <Less than
        >=Greater than or equal
        <=Less than or equal

      Comparison Operators

      Demonstrates different comparison operators and best practices

      // Equal to (==) - compares value only
      console.log(5 == "5"); // true
      
      
      // Strict Equal (===) - compares value and type
      console.log(5 === "5"); // false
      
      
      // Not Equal (!=)
      console.log(10 != 5); // true
      
      
      // Strict Not Equal (!==)
      console.log(10 !== "10"); // true
      
      
      // Greater Than (>)
      console.log(10 > 5); // true
      
      
      // Less Than (<)
      console.log(3 < 7); // true
      
      
      // Greater Than or Equal (>=)
      console.log(5 >= 5); // true
      
      
      // Less Than or Equal (<=)
      console.log(4 <= 6); // true
      
      
      // Best Practice:
      // Always prefer === and !==
      // Avoid using == and !=
      • Reason: Prevents unexpected type conversion bugs.

        Logical Operators

        What Are Logical Operators ?

        Logical operators are used to:

        • Combine conditions

        • Make decisions

        • Control program flow

        They return boolean values.

      Logical Operators

      AND, OR, and NOT operators used for logical conditions.

      // Logical AND (&&) - true only if both conditions are true
      let age = 20;
      let hasID = true;
      console.log(age >= 18 && hasID); // true
      
      
      // Logical OR (||) - true if at least one condition is true
      let isAdmin = false;
      let isUser = true;
      console.log(isAdmin || isUser); // true
      
      
      // Logical NOT (!) - reverses the result
      let isLoggedIn = false;
      console.log(!isLoggedIn); // true
      • Operator Precedence 

        JavaScript follows priority rules:

        1. Arithmetic

        2. Comparison

        3. Logical

      Understanding Operator Precedence

      Demonstrates how JavaScript evaluates expressions based on operator priority.

      // Arithmetic > Comparison > Logical
      
      console.log(5 + 5 > 8 && 10 > 3); // true
      • Common Beginner Mistakes

        • Confusing == and ===

        • Forgetting operator precedence

        • Mixing strings and numbers unknowingly

        • Incorrect logical conditions