Skip to content

JavaScript Operators

Javascript operators are used to perform different types of mathematical and logical computations.

  1. Arithmetic Operators

  2. Assignment Operators

  3. Comparison Operators

  4. Logical Operators

  5. Ternary (Conditional) Operator

  6. Bitwise Operators

Arithmetic Operators

An arithmetic operator accepts numerical values as operands and returns a single numerical value. The numerical values can be literals or variables.

  • Addition (+) : Takes two numerical operands and gives their numerical sum. It also concatenates two strings or numbers.
let sum = 10 + 20;
console.log(sum); // 30
  • Subtraction(-) : It gives the difference between two operands in the form of numerical value.
let result = 30 - 10;
console.log(result); // 20
  • Multiplication(*) : Gives the product of operands where one operand is a multiplicand and another is multiplier.
let product = 2 * 3;
console.log(product); // 6
  • Division(/) : provides the quotient of its operands where the right operand is the divisor and the left operand is the dividend.
let divide = 20 / 10;
console.log(divide); // 2
  • Modulus (%) : The modulus operator returns the remainder left over when a dividend is divided by a divisor. The modulus operator is also known as the remainder operator.
let x = 9 % 5;
console.log(x); // 4
  • Exponentiation ( ** ) : The exponentiation operator gives the result of raising the first operand to the power of the second operand.
let y = -(4 ** 2);
console.log(y); // -16
  • Increment (++) : The increment operator increments (adds one to) its operand and returns a value.
// Postfix
let a = 2;
let b = a++; // b = 2, a = 3
// Prefix
let c = 5;
let d = ++c; // c = 6, d = 6
  • Decrement (—) : The decrement operator decrements (subtracts one from) its operand and returns a value.
// Prefix
let a = 2;
b = --a;
// Postfix
let c = 3;
d = c--;
  • Unary Negation (-) : This is a unary operator i.e. it operates on a single operand. It gives the negation of an operand.
let i = 3;
j = -i; // -3
  • Unary Plus (+) : This is a way to convert a non-number into a number.
let a = +"2"; // 2
let b = +true; // 1
let c = +false; // 0
let d = +null; // 0

Assignment Operators

In JavaScript, an assignment operator (=) assigns a value to a variable.

a = b // a = b Assigns the value of b to a.
a += b // a = a + b Assigns the result of a + b to a.
a -= b // a = a - b Assigns the result of a - b to a.
a *= b // a = a * b Assigns the result of a x b to a.
a /= b // a = a / b Assigns the result of a/b to a.
a %= b // a = a % b Assigns the remainder of a and b to a.
a &=b // a = a & b Assigns the result of a AND b to a.
a |=b // a = a | b Assigns the result of a OR b to a.
a ^=b // a = a ^ b Assigns the result of a XOR b to a.
a <<= b // a = a << b Assigns the result of a shifted left by b to a.
a >>= b // a = a >> b Assigns the result of a shifted right (sign preserved) by b to a.
a >>>= b // a = a >>> b Assigns the result of a shifted right by b to a.

Comparison Operators

To compare two values, you use a comparison operator,

  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to
  • == equal to
  • != not equal to
  • === strictly equal to
  • !== strictly not equal to
let num1 = 20 > 10; // true
let num2 = 20 < 10; // false
let num3 = 10 == 10; // true
let num4 = 10 === '10'; // false
let num5 = 10 !== '10'; // true
let num6 = 20 >= 20; // true
let num7 = 20 <= 10; // false

Logical Operators

Logical operators in JavaScript are used to perform logical operations on values and return either true or false. These operators are commonly used in decision-making statements like if or while loops to control the flow of execution based on conditions.

  • && Logical AND
  • || Logical OR
  • ! Logical NOT
let age = 20;
let pass = true;
// Logical AND checks both conditions
if (age >= 18 && pass) {
console.log("all conditions true");
}
if (age >= 21 || pass) {
console.log("any one condition true");
}
console.log(!pass); // false (if true then false , if false then true)

Ternary Operator

The Ternary Operator in JavaScript is a shortcut for writing simple if-else statements. It’s also known as the Conditional Operator because it works based on a condition. The ternary operator allows you to quickly decide between two values depending on whether a condition is true or false.

let age = 22
let res = (age > 18) ? "Allow" : "Reject";
console.log(res) // Allow