In the JavaScript programming language, comparisons between values are an important part of the programming process. It plays a role in deciding the branching behavior to be performed.
Equal (==) comparison is used to check if two values are equal.
var x = 5;
var y = 10;
console.log(x == y); // false
console.log(x == 5); // true
The not equal operator (!=) is used to check if two values are different or not.
var x = 5;
var y = 10;
console.log(x != y); // true
console.log(x != 5); // false
Strict comparison (===) is used to check if two values are equal and of the same data type.
var x = 5;
var y = '5';
console.log(x === y); // false
console.log(x === 5); // true
Non-strict inequality (!==) is used to check if two values are different or different data types. For example:
var x = 5;
var y = '5';
console.log(x !== y); // true
console.log(x !== 5); // false
Greater than (>) comparison is used to check if a value is greater than another value. Less than (<) comparison is used to check if a value is less than another value.
var x = 5;
var y = 10;
console.log(x > y); // false
console.log(x < y); // true
Greater than or equal to (>=) comparison is used to check if a value is greater than or equal to another value. Less than or equal to (<=) comparison is used to check if a value is less than or equal to another value.
var x = 5;
var y = 10;
console.log(x >= y); // false
console.log(x <= y); // true
In JavaScript, we can combine multiple comparison expressions together to create logical comparison expressions. We have three main logical comparison operators:
var x = 5;
var y = 10;
var z = 15;
console.log(x < y && y < z); // true
console.log(x < y || y > z); // true
console.log(!(x == y)); // true