Operators In Javascript

Posted on December 8, 2024 by Vishesh Namdev
Python C C++ Javascript Java
Operators in JS

What is Operators

Operators are used to perform operations on variables and values. They are used to assign values to variables, compare values, perform arithmetic operations, and more. There are several types of operators in JavaScript, including assignment operators, comparison operators, arithmetic operators, logical operators, and bitwise operators.

Types of Operators

There are several types of operators in JavaScript, including:

  • Arithmetic operators: These operators are used to perform arithmetic operations such as addition, subtraction, multiplication, and division. For example, the addition operator (+) is used to add two numbers, while the subtraction operator (-) is used to subtract one number from another.
  • Arithmetic Operators in JS

    Example

    // Arithmetic Operators
          let a = 10;
          let b = 3;
          console.log("Addition: ", a + b); // 13
          console.log("Subtraction: ", a - b); // 7
          console.log("Multiplication: ", a * b); // 30
          console.log("Division: ", a / b); // 3.333...
          console.log("Modulus: ", a % b); // 1
          console.log("Exponentiation: ", a ** b); // 1000
          console.log("Increment: ", ++a); // 11
          console.log("Decrement: ", --b); // 2
  • Assignment operators: These operators are used to assign a value to a variable. For example, the assignment operator (=) is used to assign the value of the right-hand side to the left-hand side.
  • Assignement Operator in JS

    Example

    // Assignment Operators
          let c = 5;
          c += 3; // c = c + 3
          console.log("Add and assign: ", c); // 8
          c -= 2; // c = c - 2
          console.log("Subtract and assign: ", c); // 6
          c *= 2; // c = c * 2
          console.log("Multiply and assign: ", c); // 12
          c /= 3; // c = c / 3
          console.log("Divide and assign: ", c); // 4
          c %= 3; // c = c % 3
          console.log("Modulus and assign: ", c); // 1
  • Comparison operators: These operators are used to compare two values. For example, the equality operator (==) is used to check if two values are equal, while the inequality operator (! =) is used to check if two values are not equal.
  • Comparision operator in JS

    Example

    // Comparison Operators
        let x = 10, y = 20;
        console.log("Equal: ", x == y); // false
        console.log("Not Equal: ", x != y); // true
        console.log("Strict Equal: ", x === 10); // true
        console.log("Strict Not Equal: ", x !== "10"); // true
        console.log("Greater Than: ", x > y); // false
        console.log("Less Than: ", x < y); // true
        console.log("Greater or Equal: ", x >= 10); // true
        console.log("Less or Equal: ", x <= 10); // true
  • 4. Logical operators: These operators are used to perform logical operations such as AND, OR, and NOT. For example, the AND operator (&&) is used to check if both conditions are true , while the OR operator (||) is used to check if at least one condition is true.
  • Logical Operator in JS

    Example

    // Logical Operators
        let isTrue = true, isFalse = false;
        console.log("AND: ", isTrue && isFalse); // false
        console.log("OR: ", isTrue || isFalse); // true
        console.log("NOT: ", !isTrue); // false
  • Bitwise operators: These operators are used to perform bitwise operations such as AND, OR, and XOR. For example, the AND operator (&) is used to perform a bitwise AND operation, while the OR operator (|) is used to perform a bitwise OR operation.
  • Example

    // Bitwise Operators
      let bit1 = 5; // 0101 in binary
      let bit2 = 3; // 0011 in binary
      console.log("Bitwise AND: ", bit1 & bit2); // 0001 (1 in decimal)
      console.log("Bitwise OR: ", bit1 | bit2); // 0111 (7 in decimal)
      console.log("Bitwise XOR: ", bit1 ^ bit2); // 0110 (6 in decimal)
      console.log("Bitwise NOT: ", ~bit1); // -(5+1) = -6
      console.log("Left Shift: ", bit1 << 1); // 1010 (10 in decimal)
      console.log("Right Shift: ", bit1 >> 1); // 0010 (2 in decimal)
      console.log("Unsigned Right Shift: ", bit1 >>> 1); // 2
  • Ternary operators: A ternary operator is a type of conditional operator that takes three operands. It is often used as a shorthand for an if-else statement.
  • Ternary Operator in JS

    Example

    // Ternary Operator
      let age = 18;
      let canVote = (age >= 18) ? "Yes" : "No";
      console.log("Can Vote: ", canVote); // Yes
    📢Important Note📢