You are currently viewing JavaScript Cheat Sheet 10.29: JavaScript Comparison and Logical Operators

JavaScript Cheat Sheet 10.29: JavaScript Comparison and Logical Operators

Mastering JavaScript Comparison and Logical Operators


Programming is all about making decisions and controlling the flow of your code. In JavaScript, comparison and logical operators are fundamental tools that allow you to create conditions and execute specific code based on those conditions. This tutorial will walk you through the basics of JavaScript comparison and logical operators, providing clear explanations and sample codes to help you grasp these essential concepts.

Understanding JavaScript Comparison Operators:

1. Equal (==) Operator:

The equal operator checks if two values are equal.

let x = 5;
let y = "5";
console.log(x == y); // true

In this example, even though the types are different, the values are equal, so the output will be true.

2. Strict Equal (===) Operator:

The strict equal operator checks both value and type.

let a = 5;
let b = "5";
console.log(a === b); // false

Here, the strict equal operator considers both value and type, so the output is false.

3. Not Equal (!=) Operator:

The not equal operator checks if two values are not equal.

let p = 10;
let q = 20;
console.log(p != q); // true

As the values are not equal, the output will be true.

4. Strict Not Equal (!==) Operator:

The strict not equal operator checks both value and type.

let m = 10;
let n = "10";
console.log(m !== n); // true

The strict not equal operator considers both value and type, so the output is true.

5. Greater Than (>) Operator:

The greater than operator checks if the left operand is greater than the right operand.

let num1 = 15;
let num2 = 10;
console.log(num1 > num2); // true

In this case, num1 is greater than num2, so the output is true.

6. Less Than (<) Operator:

The less than operator checks if the left operand is less than the right operand.

let val1 = 5;
let val2 = 8;
console.log(val1 < val2); // true

Here, val1 is less than val2, so the output is true.

7. Greater Than or Equal To (>=) Operator:

The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.

let a1 = 10;
let a2 = 10;
console.log(a1 >= a2); // true

As a1 is equal to a2, the output is true.

8. Less Than or Equal To (<=) Operator:

The less than or equal to operator checks if the left operand is less than or equal to the right operand.

let b1 = 15;
let b2 = 20;
console.log(b1 <= b2); // true

In this case, b1 is less than b2, so the output is true.

Understanding JavaScript Logical Operators:

1. Logical AND (&&) Operator:

The logical AND operator returns true if both operands are true.

let condition1 = true;
let condition2 = false;
console.log(condition1 && condition2); // false

Here, since condition1 is true and condition2 is false, the output is false.

2. Logical OR (||) Operator:

The logical OR operator returns true if at least one of the operands is true.

let condition3 = true;
let condition4 = false;
console.log(condition3 || condition4); // true

As condition3 is true, the output is true.

3. Logical NOT (!) Operator:

The logical NOT operator negates the given condition.

let isTrue = true;
console.log(!isTrue); // false

Here, !isTrue negates the value of isTrue, so the output is false.

Conclusion:

Understanding JavaScript comparison and logical operators is crucial for writing effective and conditional code. By mastering these concepts, you gain the ability to control the flow of your programs and make decisions based on various conditions. Practice these operators in different scenarios to reinforce your understanding and enhance your programming skills.

Happy coding!

Leave a Reply