JavaScript Comparison and Logical Operators
Operators are a core part of any programming language. They help developers make decisions in their code, compare values, and perform logical operations. In JavaScript, comparison and logical operators play a crucial role in decision-making and controlling the flow of code.
Comparison operators are used to compare two values or expressions. These operators return a boolean value (true
or false
) based on the comparison result.
==
)===
)!=
)!==
)>
)<
)>=
)<=
)Let’s dive into each of these comparison operators with examples.
==
)The ==
operator compares two values for equality, but it performs type coercion. This means that JavaScript will convert the values to the same type before making the comparison.
console.log(5 == "5"); // Output: true (string "5" is converted to number 5)
console.log(0 == false); // Output: true (false is converted to 0)
===
)The ===
operator checks if two values are equal and of the same type. It does not perform type coercion.
console.log(5 === "5"); // Output: false (different types)
console.log(5 === 5); // Output: true (same type and value)
!=
)The !=
operator checks if two values are not equal, but it allows type coercion.
console.log(5 != "5"); // Output: false (converted to same type, so values are equal)
console.log(0 != false); // Output: false (false is converted to 0)
!==
)The !==
operator checks if two values are not equal or not of the same type. It does not perform type coercion.
console.log(5 !== "5"); // Output: true (different types)
console.log(5 !== 5); // Output: false (same type and value)
>
)The >
operator compares if the value on the left is greater than the value on the right.
console.log(10 > 5); // Output: true
console.log(5 > 10); // Output: false
<
)The <
operator compares if the value on the left is less than the value on the right.
console.log(3 < 7); // Output: true
console.log(8 < 5); // Output: false
>=
)The >=
operator checks if the value on the left is greater than or equal to the value on the right.
console.log(7 >= 5); // Output: true
console.log(3 >= 5); // Output: false
<=
)The <=
operator checks if the value on the left is less than or equal to the value on the right.
console.log(5 <= 5); // Output: true
console.log(6 <= 4); // Output: false
Logical operators are used to combine multiple conditions in JavaScript. They allow developers to make more complex decisions in code. JavaScript provides three primary logical operators:
&&
)||
)!
)Let's look at each operator with examples.
&&
)The &&
operator returns true
if both operands are truthy. If any operand is falsy, it returns false
.
console.log(true && true); // Output: true
console.log(true && false); // Output: false
console.log(false && true); // Output: false
console.log(false && false); // Output: false
let x = 5;
let y = 10;
console.log(x > 2 && y < 15); // Output: true (both conditions are true)
console.log(x > 10 && y < 15); // Output: false (first condition is false)
||
)The ||
operator returns true
if any one of the operands is truthy. If both operands are falsy, it returns false
.
console.log(true || false); // Output: true
console.log(false || false); // Output: false
console.log(true || true); // Output: true
console.log(false || true); // Output: true
let x = 5;
let y = 10;
console.log(x < 10 || y > 15); // Output: true (first condition is true)
console.log(x > 10 || y > 15); // Output: false (both conditions are false)
!
)The !
operator is used to negate a boolean value. It returns false
if the value is truthy, and true
if the value is falsy.
console.log(!true); // Output: false
console.log(!false); // Output: true
console.log(!(5 > 3)); // Output: false (because 5 > 3 is true, and negating it gives false)
let x = 5;
console.log(!(x > 3)); // Output: false (because x > 3 is true, negating it gives false)
console.log(!(x < 3)); // Output: true (because x < 3 is false, negating it gives true)
When combining comparison and logical operators in a single expression, it's important to understand operator precedence. The precedence determines the order in which operators are evaluated.
==
, ===
, !=
, !==
, <
, >
, <=
, >=
) have higher precedence than logical operators (&&
, ||
, !
).!
) has higher precedence than AND (&&
) and OR (||
).
let x = 5;
let y = 10;
let z = 15;
console.log(x < y && y < z); // Output: true (evaluates x < y first, then y < z, and combines with AND)
console.log(x < y || y > z); // Output: true (evaluates x < y first, then y > z, combines with OR)