JS Comparison and Logical Operators

JS Comparison and Logical Operators

JS Comparison and Logical Operators

 

In JavaScript, there are several comparison operators and logical operators that you can use to compare values and create Boolean expressions.

Comparison operators:

  • == : Equal to
  • === : Equal to (strict equality, compares types as well)
  • != : Not equal to
  • !== : Not equal to (strict inequality, compares types as well)
  • < : Less than
  • > : Greater than
  • <= : Less than or equal to
  • >= : Greater than or equal to

Examples:

				
					const x = 5;
const y = 10;

console.log(x == y); // false
console.log(x != y); // true
console.log(x < y); // true
console.log(x > y); // false
console.log(x <= y); // true
console.log(x >= y); // false 

				
			

Logical operators:

  • && : And (returns true if both operands are true)
  • || : Or (returns true if at least one operand is true)
  • ! : Not (reverses the Boolean value of an expression)

Examples:

				
					const x = 5;
const y = 10;
const z = 15;

console.log(x < y && y < z); // true
console.log(x > y || y > z); // false
console.log(!(x > y)); // true

				
			

Note that the && and || operators use short-circuit evaluation. This means that if the first operand of && is false, or the first operand of || is true, the second operand is not evaluated. This can be useful for optimizing code and avoiding unnecessary computations.

Join To Get Our Newsletter
Spread the love