DEV Community

Sasireka
Sasireka

Posted on

Operators

1. Operators in JavaScript

  • JavaScript uses various operators to perform operations on values and variables.

  • These operators are categorized based on the type of operation they perform, such as arithmetic, assignment, comparison, and logical operations.

Arithmetic Operator: Used to perform mathematical operations.

  • + (Addition) //let x = 100 + 50;

  • - (Subtraction / Unary negation) //let y = 100 - 50;

  • * (Multiplication) //let x = 2 * 3;

  • / (Division) //let x = 50 / 5;

  • % (Remainder/Modulus) //let y = 7 % 2;

  • ++ (Increment) //i++ or ++i;

  • -- (Decrement) //i--; or --i

Assignment Operator: Assign values to variables, often with a combined operation.

  • = (Assignment) //x = y

  • += (Addition assignment) //x += y => x = x + y

  • -= (Subtraction assignment) //x -= y => x = x - y

  • *= (Multiplication assignment) //x *= y => x = x * y

  • /= (Division assignment) //x /= y => x = x / y

  • %= (Remainder assignment) //x %= y => x = x % y

  • **= (Exponentiation assignment) //x **= y => x = x ** y

  • &&= (Logical AND assignment) //x &&= y => x && (x = y)
    If x is true, assign y to x
    If x is false, do nothing

  • ||= (Logical OR assignment) //x ||= y => x || (x = y)
    If x is false, assign y to x
    If x is true, do nothing

Comparison Operator: Compare two values and return a boolean (true or false).
Assume : let x = 5;

  • == (Equal, checks value only; performs type coercion)

//x == 8 o/p: false
//x == "5" o/p: true

  • === (Strict equal, checks value and type)

x === 5 o/p: true
x === "5" o/p: false

  • != (Not equal) //x != 8 o/p: true

  • !== (Strict not equal, checks value and type)

x !== 5 o/p: false
x !== "5" o/p: true
x !== 8 o/p: true

  • > (Greater than) //x > 8 o/p: false

  • < (Less than) //x < 8 o/p: true

  • >= (Greater than or equal to) //x >= 8 o/p: false

  • <= (Less than or equal to) //x <= 8 o/p: true

Logical Operator: Combine multiple boolean conditions.
Assume: x = 6 and y = 3

  • && (Logical AND) //(x < 10 && y > 1) o/p: true

  • || (Logical OR) //(x === 5 || y === 5) o/p: false

  • ! (Logical NOT) //!(x === y) o/p: true

Top comments (0)