Mastering Comparison Operators in JavaScript: Equality, Relational, and Logical


In JavaScript, comparison operators are used to compare two values and return a Boolean value (true or false) depending on the result of the comparison. The basic comparison operators include

Equality (==): This operator compares two values to see if they are equal.

Inequality (!=): This operator compares two values to see if they are not equal.

=== operator, on the other hand, is a strict equality operator and does not perform type coercion. It will only return true if the values being compared have the same type and value.

Sno Operator Usage
1. == equal to
2. === equal value and equal type
3. != not equal
4. !== not equal value or not equal type

Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

The first console.log statement will output false because the == operator compares the values of the variables, and 10 is not equal to "25".

The second console.log statement will output false as well because the === operator compares the values and types of the variables, and 10 is not equal to "25" in both value and type.

The third console.log statement will output true because the != operator returns the opposite of the == operator, so 10 is not equal to "25".

The fourth console.log statement will output true as well because the !== operator returns the opposite of the === operator, so 10 is not equal to "25" in both value and type.

script.js
//Comparison Operators
 
let a=10;
let b="25";
console.log(a==b);
console.log(a===b);
console.log(a!=b);
console.log(a!==b);
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project