JavaScript Lessons

JavaScript Comparison Operators and Logical Operators Guide

Learn how JavaScript comparison operators and logical operators work with clear examples. See why == and === behave differently and how to combine conditions safely.

Serhat ALPASLAN5 min read

javascript operators

JavaScript Comparison Operators

JavaScript comparison operators compare two values and return a boolean result of true or false.

Operator Description Example Output
== Equal to: If the operands are equal to each other, the result true is returned. 2 == 3; //true
!= Not equal to: If it is true that the operands are not equal to each other, the result true is returned. 2 != 2; //false
=== Strictly equal to: If they are equal in both type and value, the result true is returned. 8 === '8'; //false
!== Strictly not equal to: If they are not equal in either type or value, the result true is returned. 5 !== '5'; //true
> Greater than: If the value on the left is greater than the value on the right, the result true is returned. 8 > 4; //true
>= Greater than or equal to: If the value on the left is equal to or greater than the value on the right, the result true is returned. 3 >= 3; //true
< Less than: If the value on the left is less than the value on the right, the result true is returned. 3 < 2; //false
<= Less than or equal to: If the value on the left is equal to or less than the value on the right, the result true is returned. 2 <= 2; //true

Example 1: Using the Equal to ( == ) Operator

const a = 31, b = 22, c = 'merhaba';

// let's log whether they are equal to the console
console.log(a == 5);     // will return true
console.log(b == '2');   // will return true
console.log(c == 'merhaba');  // will return false

Note: Be careful when using “==” among the comparison operators during JavaScript development, because many users confuse = with ==. If you use the = sign instead of ==, you may run into some different situations.

Example 2: Using the Not Equal to ( != ) Operator

const a = 3, b = 'merhaba';

// using the not equal to operator
console.log(a != 1); // true
console.log(b != 'abcde'); // true

We got the result true from != because it is true that the values are not equal to each other.

Example 3: Using the Strictly Equal to ( === ) Operator

const varA = 2;

// using the strictly equal to operator.
console.log(varA  === 2); // true
console.log(varA  === '2'); // false

In the example above, because we wrote the number 2 in quotes in the second console.log, JavaScript interpreted it as a string, so even though the values look equal, it returned false to us because their types are not equal.

Example 4: Using the Strictly Not Equal to ( !== ) Operator

const a = 5, b = 'merhaba';

// using the strictly not equal to operator
console.log(a !== 3); // false
console.log(a !== '2'); // true
console.log(b !== 'meraba'); // true

Example 5: Using the Greater Than ( > ) Operator

const z = 99;

// using the greater than operator 
console.log(z > 22); // true

Example 6: Using the Greater Than or Equal To ( >= ) Operator

const z = 99; // using the greater than or equal to operator 
console.log(z >= 99 ); // will return true.

Example 7: Using the Less Than ( < ) Operator

const z = 99; // using the less than operator 
console.log(z < 100); // true

Example 8: Using the Less Than or Equal To ( < ) Operator

const z = 100; // using the less than or equal to operator 
console.log(z <= 100); // true

JavaScript Logical Operators

JavaScript logical operators handle expressions like AND, OR and NOT.

Operator Description Example Usage
&& Logical AND: If both values are equal to each other it returns true, otherwise it returns false. true && false; // false
|| Logical OR: If either of the two values is true, the result returns true. However, if both values are false, the result returns false. true || false; // true
! Logical NOT: When ! is placed in front of a value, a true result is treated as false or vice versa. !true; // false

Example 9: Using the Logical AND Operator

const x = true, y = false;
const z = 5;

// Logical AND operator
console.log(x && x); // true
console.log(x && y);  // false

console.log((z > 2) && (z < 2)); // false

Note: Logical operators can also be used with numbers. In JavaScript, all values that are 0 are evaluated as false and all values that are not zero are evaluated as true.

Example 9: Using the Logical OR Operator

const x = true, y = false, z = 4;

// using the logical OR operator
console.log(x || y); // true
console.log(y || y); // false
console.log((z > 2) || (z < 2)); // true

If either of the values is true, it will return the result true. If both values are false, it will return a false result.

Share it if it helped

Write a comment