Instagram
youtube
Facebook
Twitter

JavaScript operators

What Are Operators?

In JavaScript, operators are special symbols used to perform operations on variables and values. They can be used for mathematical calculations, comparing values, logical operations, and more.


Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. Here are some common arithmetic operators in JavaScript:

  • Addition (+): Adds two numbers.

    let sum = 5 + 3; // sum is 8

     

  • Subtraction (-): Subtracts one number from another.

    let difference = 10 - 4; // difference is 6

     

  • Multiplication (*): Multiplies two numbers.

    let product = 7 * 2; // product is 14

     

  • Division (/): Divides one number by another.

    let quotient = 20 / 5; // quotient is 4

     

  • Modulus (%): Returns the remainder of a division operation.

    let remainder = 10 % 3; // remainder is 1

     


Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator is =. There are also compound assignment operators that combine an operation with assignment:

  • Simple Assignment (=): Assigns a value to a variable.

    let x = 10;

     

  • Add and Assign (+=): Adds a value to a variable and assigns the result.

    x += 5; // equivalent to x = x + 5

     

  • Subtract and Assign (-=): Subtracts a value from a variable and assigns the result.

    x -= 3; // equivalent to x = x - 3

     

  • Multiply and Assign (*=): Multiplies a variable by a value and assigns the result.

    x *= 2; // equivalent to x = x * 2

     

  • Divide and Assign (/=): Divides a variable by a value and assigns the result.

    x /= 4; // equivalent to x = x / 4

     


Comparison Operators

Comparison operators are used to compare two values. The result of a comparison is a boolean value (true or false).

  • Equal (==): Checks if two values are equal (type conversion is allowed).

    5 == '5'; // true

     

  • Strict Equal (===): Checks if two values are equal and of the same type.

    5 === '5'; // false

     

  • Not Equal (!=): Checks if two values are not equal.

    5 != '5'; // false

     

  • Strict Not Equal (!==): Checks if two values are not equal or not of the same type.

    5 !== '5'; // true

     

  • Greater Than (>): Checks if the value on the left is greater than the value on the right.

    7 > 5; // true

     

  • Less Than (<): Checks if the value on the left is less than the value on the right.

    3 < 4; // true

     

  • Greater Than or Equal To (>=): Checks if the value on the left is greater than or equal to the value on the right.

    5 >= 5; // true

     

  • Less Than or Equal To (<=): Checks if the value on the left is less than or equal to the value on the right.

    6 <= 10; // true

     


Logical Operators

Logical operators are used to combine multiple conditions. The most common logical operators are:

  • AND (&&): Returns true if both conditions are true.

    let a = true && true; // true

     

  • OR (||): Returns true if at least one condition is true.

    let b = true || false; // true

     

  • NOT (!): Reverses the boolean value.

    let c = !true; // false

     


Conclusion

Understanding JavaScript operators is essential for performing a wide range of operations in your code. By mastering these operators, you can manipulate data, make decisions, and implement complex logic in your applications.