Operator Types In JavaScript: Arithmetic, Comparison, Logical, and More

JavaScript operators are symbols that are used to perform operations on operands. Operators are the foundation of any programming language, enabling you to implement logic, compare variables, perform arithmetic operations, and more.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations between numeric operands.

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second.
  • Modulus (%): Returns the remainder of dividing the first operand by the second.
  • Increment (++): Increases an integer value by one.
  • Decrement (--): Decreases an integer value by one.
let result;

result = 5 + 3; // 8
result = 10 - 2; // 8
result = 4 * 2; // 8
result = 16 / 2; // 8
result = 10 % 3; // 1

let count = 0;
count++; // 1
count--; // 0

Comparison Operators

Comparison operators compare two values and return a boolean value, true or false.

  • Equal (==): Checks if the values of two operands are equal (after type conversion).
  • Strict equal (===): Checks if the values and types of two operands are equal.
  • Not equal (!=): Checks if the values of two operands are not equal.
  • Strict not equal (!==): Checks if the values and types of two operands are not equal.
  • Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=): These operators compare two values numerically.
console.log(5 == "5"); // true (type coercion)
console.log(5 === "5"); // false (strict comparison)
console.log(5 != "6"); // true
console.log(5 !== "5"); // true
console.log(5 > 3); // true
console.log(5 < 3); // false
console.log(5 >= 5); // true
console.log(5 <= 4); // false

Logical Operators

Logical operators are used to determine the logic between variables or values.

  • AND (&&): Returns true if both operands are true.
  • OR (||): Returns true if at least one of the operands is true.
  • NOT (!): Returns true if the operand is false.
let a = true, b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false

Assignment Operators

Assignment operators assign values to JavaScript variables.

  • Assignment (=): Assigns the value of the right operand to the left operand.
  • Addition assignment (+=), Subtraction assignment (-=), Multiplication assignment (*=), Division assignment (/=), Modulus assignment (%=): These perform the operation and assign the result.
let x = 10;
x += 5; // x = x + 5
x -= 3; // x = x - 3
x *= 2; // x = x * 2
x /= 4; // x = x / 4
x %= 2; // x = x % 2

Ternary Operator

The ternary operator is a shorthand for the if-else statement and is represented by ? :.

let age = 19;
let canVote = (age >= 18) ? "Yes" : "No";
console.log(canVote); // Yes

Notes:

  • Type Coercion: JavaScript is known for type coercion in comparison operators (== and !=), where it converts the operands to the same type before making the comparison.
  • Precedence: Operator precedence determines the order in which operations are performed. For instance, multiplication and division are performed before addition and subtraction.

Understanding these operators and how they work is essential for controlling program flow, performing calculations, and making decisions in your JavaScript code.