Operators in JavaScript

  • Operators are symbols or keywords in JavaScript that perform operations on one or more operands (values or variables). JavaScript supports a wide range of operators, including arithmetic, comparison, logical, bitwise, assignment, and conditional operators.
Here is a list of all the operators in JavaScript, along with their precedence rules and examples:
  • Arithmetic Operators: Arithmetic operators in JavaScript are used to perform mathematical operations on numbers.
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Remainder (%)
    • Exponentiation (**)
    • Increment (++)
    • Decrement (--)
  • For example:

    var a = 10;
    var b = 5;
    var c = a + b; // 15
    var d = a - b; // 5
    var e = a * b; // 50
    var f = a / b; // 2
    var g = a % b; // 0
    var h = a ** b; // 100000
    var i = ++a; // 11
    var j = a++; // 11
    var k = --b; // 4
    var l = b--; // 4

  • The arithmetic operators use with same priority in an expression then solve left-to-right associativity.
  • It is higher precedence than the comparison operators.
  • When multiple arithmetic operators are used in an expression, the order in which they are evaluated is determined by operator precedence. Operator precedence determines which operators are evaluated first. The following table shows the operator precedence for arithmetic operators in JavaScript:
    • Parentheses can be used to override the default operator precedence. For example, the expression 5 * 2 + 3 will be evaluated as 10 + 3, because multiplication has a higher precedence than addition. However, the expression (5 * 2) + 3 will be evaluated as 10 + 3, because the parentheses override the default operator precedence and force the multiplication to be evaluated first.

        console.log(5 * 2 + 3)  // 13
        console.log(5 * (2 + 3))   // 25

    • Arithmetic operators can be used to perform a variety of mathematical operations, such as calculating the area of a rectangle, the volume of a sphere, or the interest on a loan. They can also be used to create more complex mathematical expressions, such as finding the maximum or minimum value in a list of numbers.
    • Comparison Operators: It is also known as relational operators. It is used to compare two values and return a Boolean value (true or false).
      • Equal (==)
      • Not equal (!=)
      • Strict equal (===)
      • Strict not equal (!==)
      • Greater than (>)
      • Greater than or equal to (>=)
      • Less than (<)
      • Less than or equal to (<=)
    • The comparison operators have left-to-right associativity and have a lower precedence than the arithmetic operators. For example:

        console.log(5 == 5)  // true
        console.log(5 == '5')  // true
        console.log(5 === 5)  // true
        console.log(5 === '5')  // false

        console.log(5 != 5)  // false
        console.log(5 != '5')  // false
        console.log(5 !== 5)  // false
        console.log(5 !== '5')  // true

        console.log(5 > 7)  // false
        console.log(5 >= 7)  // false

        console.log(5 < 7)  // true
        console.log(5 <= 7)  // true

    • Logical Operators: The logical operators in JavaScript are used to perform logical operations on boolean values (true or false). There are three logical operators in JavaScript: && (logical AND), || (logical OR), and ! (logical NOT). Here's an explanation of each operator with examples:
      • AND (&&): Returns true if both operands are true. Otherwise, it returns false.
      • OR (||): Returns true if either operand is true. Otherwise, it returns false.
      • NOT (!): Returns the opposite of the operand.
    • The logical operators have left-to-right associativity and have a lower precedence than the comparison operators. For example:

        var a = true;
        var b = false;
        var c = a && b; // false
        var d = a || b; // true
        var e = !a; // false

    • Logical operators are a powerful tool that can be used to control the flow of your code. By understanding how they work, you can write more efficient and effective JavaScript programs.
    • Bitwise Operators: Bitwise operators in JavaScript are used to perform operations on bits, which are the individual 0s and 1s that make up a number. There are seven bitwise operators in JavaScript:
      • AND (&)
      • OR (|)
      • XOR (^)
      • NOT (~)
      • Left shift (<<)
      • Right shift (>>)
      • Unsigned right shift (>>>)
    • The bitwise operators have left-to-right associativity and have a lower precedence than the logical operators. For example:

        var a = 5;
        var b = 3;
        var c = a & b; // 1
        var d = a | b; // 7
        var e = ~a; // -6
        var f = a << 1; // 10
        var g = b >> 1; // 1

    • Assignment Operators:
      • Assignment (=)
      • Addition assignment (+=)
      • Subtraction assignment (-=)
      • Multiplication assignment (*=)
      • Division assignment (/=)
      • Remainder assignment (%=)
      • Left shift assignment (<<=)
      • Right shift assignment (>>=)
      • Unsigned right shift assignment (>>>=)
      • Bitwise AND assignment (&=)
      • Bitwise OR assignment (|=)
      • Bitwise XOR assignment (^=)
    • The assignment operators have right-to-left associativity and have a lower precedence than the bitwise operators. For example:
        var a = 10;
        a += 5; // a = a + 5;
        var b = 3;
        b <<= 1; // b = b << 1;
    • Conditional Operator:
      • Ternary operator (condition ? expr1 : expr2)
    • The conditional operator is a ternary operator that has right-to-left associativity and has a lower precedence than the assignment operators. It takes three operands - a condition, a value to return if the condition is true, and a value to return if the condition is false. For example:
        var a = 10;
        var b = (a > 5) ? "greater than 5" : "less than or equal to 5";
    • In this example, if the condition a > 5 is true, the value of b will be "greater than 5". Otherwise, it will be "less than or equal to 5".
    • Comma Operator:
      • Comma (,)
    • The comma operator is used to separate expressions in a statement and has left-to-right associativity and has the lowest precedence among all the operators. It evaluates each of its operands from left to right and returns the value of the rightmost operand. For example:
        var a = 10, b = 20, c = 30;
        var d = (a++, b++, c++); // 30
    • In this example, the comma operator evaluates the expressions a++, b++, and c++ in that order and returns the value of c++, which is 30.

    No comments:

    Post a Comment