JavaScript Operators at a glance


JavaScript offers a variety of operators that can be categorized into several types:

Arithmetic Operators

Back to Top ^

Addition (+)

5 + 3 // equals 8
  • Adds two values

Subtraction (-)

5 - 3 // equals 2
  • Subtracts the second value from the first

Multiplication (*)

5 * 3 // equals 15
  • Multiplies two values

Division (/)

6 / 3 // equals 2
  • Divides the first value by the second

Modulus (%)

7 % 3 // equals 1, because 7 divided by 3 leaves a remainder of 1
  • Returns the remainder of a division.

Increment (++)

If x = 5, then x++ or ++x equals 6
  • Increases the value by 1

Decrement (–)

If x = 5, then x-- or --x equals 4
  • Decreases the value by 1

Exponentiation (**)

2 ** 3 equals 8 (2 to the power of 3)
  • Raises a number to the power of another

Comparison Operators

Back to Top ^

Equal (==)

"3" == 3 // is true.
  • Checks if two values are equal, without type checking

Strictly equal (===)

"3" === 3 // is false
  • Checks if two values are equal and of the same type

Not equal (!=)

"3" != 3 // is false
  • Checks if two values are not equal, without type checking

Strictly not equal (!==)

"3" !== 3 // is true
  • Checks if two values are not equal or not of the same type

Greater than (>)

5 > 3 // is true
  • Checks if the left value is greater than the right one

Less than (<)

5 < 3 // is false
  • Checks if the left value is less than the right one

Greater than or equal (>=)

5 >= 5 // is true
  • Checks if the left value is greater than or equal to the right one

Less than or equal (<=)

5 <= 5 // is true
  • Checks if the left value is less than or equal to the right one

Logical Operators

Back to Top ^

And (&&)

(5 > 3) && (3 > 1) // is true
  • Returns true if both operands are true

Or (||)

(5 > 3) || (3 < 1) // is true
  • Returns true if at least one of the operands is true

Not (!)

!(5 > 3) // is false
  • Reverses the truth value

Assignment Operators

Back to Top ^

Assignment (=)

let x = 5 // assigns the value 5 to the variable x
  • Assigns the right value to the left operand

Addition assignment (+=)

x += 3 // is equivalent to x = x + 3
  • Adds the right to the left operand and assigns the result to the left operand

Subtraction assignment (-=)

x -= 3 // is equivalent to x = x - 3
  • Subtracts the right from the left operand and assigns the result to the left operand

Multiplication assignment (*=)

x *= 3 // is equivalent to x = x * 3
  • Multiplies the left by the right operand and assigns the result to the left operand

Division assignment (/=)

x /= 3 // is equivalent to x = x / 3
  • Divides the left operand by the right and assigns the result to the left operand

Modulus assignment (%=)

x %= 3 // is equivalent to x = x % 3
  • Calculates the modulus of the left and right operands and assigns the result to the left operand

Exponentiation assignment (**=)

x **= 3 // is equivalent to x = x ** 3
  • Raises the left operand to the power of the right and assigns the result to the left operand

Special Operators

Back to Top ^

Ternary Operator (?:)

let result = (x > 10) ? "Greater than 10" : "Less than or equal to 10";
  • Conducts a quick check and assigns one of two values depending on the result (similar to an if-else structure)

Spread Operator (…)

let parts = ['shoulders', 'knees']; let lyrics = ['head', ...parts, 'and', 'toes'];
  • Used to expand elements of an array or object properties

Destructuring Assignment

let [a, b] = [1, 2]; console.log(a); // 1
  • Allows assigning properties of an array or object to variables in a single expression

These operators are central to JavaScript programming as they enable expressing complex logic in a simple and efficient way.

Leave a Comment