Comparison Operators in javascript

Comparison Operators in javascript

Comparison operators in JavaScript programming are used to compare two different program data values. Comparison operators return a boolean value as output in JavaScript programs. This value can be true or false, which determines whether the comparison of data type values ​​is correct or not.

Comparison Operators in javascript

So let’s go through the common comparison operators in JavaScript programs.

Equals (==) operator.

In any JavaScript program, the equals == operator checks whether two data type values ​​are equal or not, where it is after type coercion, which means that JavaScript equal operator will try to make the data type values ​​equal before comparing.

Example of Equals Operator in JavaScript Program.

let p = 1;

let q = ‘1’;

console.log(p == q); // result – true (string ‘1’ is equated to numeric 1)

Type coercion – Here even if one data type value is a numeric (5) and the other data type value is a string (‘5’) value, here JavaScript replaces the string ‘5’ with a numeric data before comparing the data type values.

Strict Equals (===) Operator.

The Strict Equals JavaScript === operator in the program checks if two JavaScript program data values ​​are equal, and both the data values ​​are similar in nature. It does not apply coercion to the existing data type value.

Example of Strict Equals in JavaScript program.

let p = 2;

let q = ‘2’;

console.log(p === q); // result – false (multiple data type values ​​are numeric and string data types)

There is no type coercion here. In the above example, 2 is a numeric data type value and ‘2’ is a string data type value, so the output is false even if their storage value is equal.

Not Equal (!=) Operator.

In JavaScript program, != operator checks if two data type values ​​in the current program are not equal, it checks the value with the coercion of data type value.

Not Equal (!=) Operator Example in JavaScript.

let p = 3;

let q = ‘3’;

console.log(p != q); // result – False (Here string ‘3’ is forcefully added to number 3, so they are equal)

Type coercion – Like equal == operator, it will force the data type values ​​to the same type before comparing in JavaScript program.

Strict Not Equals (!==) operator.

Strict Not Equals !== operator in JavaScript program checks if two data type values ​​in the current program are equal or of the same type. It does not force the data type to be a value.

Example of Strict Not Equals (!==) operator in JavaScript.

let p = 4;

let q = ‘4’;

console.log(p !== q); // result – True (Multiple data type – Numeric and String value)

No type coercion – Here in the current program, even though the data type values ​​are equal but the data types are different (Numeric vs String) so the result is True.

Greater Than (>) operator.

Greater Than > operator in JavaScript program checks if the left program operand is Greater Than value than the right program operand.

Example of Greater Than (>) operator in JavaScript program.

let p = 2;

let q = 1;

console.log(p > q); // result – true (where 2 is a value greater than 1)

Less Than (<) operator.

Less Than < operator in JavaScript checks whether the left program operand in the current program is less than the right program operand.

Example of Less Than (<) operator in JavaScript.

let p = 4;

let q = 11;

console.log(p < q); // result – true (4 is a value less than 11)

Greater Than or Equal To (>=) operator.

Greater Than or Equal To >= operator in JavaScript checks whether the left program operand is greater than or equal to the right program operand.

Example of Greater Than or Equal To (>=) operator in JavaScript.

let p = 3;

let q = 3;

console.log(p >= q); // result – true (Here both variables 3 are equal to 3)

Less than or equal to (<=) operator.

Less than and equal to <= operator in JavaScript program checks whether the left operand in the current program is less than or equal to the right program operand.

Example of Less than or equal to (<=) operator in JavaScript.

let p = 7;

let q = 9;

console.log(p <= q); // result – true (Here 7 is a value less than 9)

NaN (Not a Number) comparison operator.

In JavaScript program, NaN data type value is a special value which means “not a value”, and this value is not equal to anything, including its own value.

Example of NaN (Not a Number) comparison operator in JavaScript program.

let p = NaN;

let q = NaN;

console.log(p == q); // result – false (Here NaN data type is not equal to anything, not even its own value)

console.log(p === q); // result – false (This NaN is not equal to anything, not even its own value)

If you need to check whether a value is NaN or not in a JavaScript program, then you have to use isNaN() function in JavaScript program.

let a = NaN;

console.log(isNaN(p)); // result – true (p is NaN)

Summary of Comparison Operators in javascript.

OperatorDescriptionExampleResult
==This operator used to test Equal to (with type coercion)1 == ‘1’true
===This operator used to text Strict equal to (without type coercion)2 === ‘3’false
!=This operator used to test Not equal to (with type coercion)3 != ‘3’false
!==This operator used to test Strict not equal to (without type coercion)4 !== ‘3’true
This operator used to test check Greater than value2 > 1true
This operator used to check Less than value4 < 12true
>=This operator used to check Greater than or equal to value1 >= 1true
<=This operator used to test Less than or equal to value7 <= 9true
NaNThis operator used to check NaN is never equal to anything, including itself operatorNaN == NaNfalse

Edge cases to consider in JavaScript.

Type Coercion – When using equal == and not equal != operators, JavaScript does data type coercion. So sometimes you may get unexpected results. When you want to avoid coercion in a program, and want to compare both data values ​​and data types, you can always use === and !==.

Comparing NaN – NaN data type value is a special case in JavaScript programs, because it is not equal to any data type value or thing in the JavaScript program, including itself. You can always use isNaN() function to test NaN data type in JavaScript programs.

String and number comparison – When comparing a string with a numeric using the equals == operator in JavaScript programs, it will attempt to convert the string to a numeric in the JavaScript program. Whereas, with ===, if the data types are different, the comparison of data values ​​will return false.

Example of String and number comparison in JavaScript.

let p = “13”;

let q = 13;

console.log(p == q); // result – true (This is string ’13’ being forced to be numeric 13)

console.log(p === q); // result – false (Here values ​​are of different data types – string and numeric data types)

Conclusion of Comparison Operators in JavaScript.

Comparison operators in JavaScript programming are important for evolving conditions and controlling or managing the flow of your program. It is important to analyse the main difference between == and === (type coercion vs strict comparison) operators to avoid unexpected results in a JavaScript program. When comparing data type values ​​in JavaScript programs, you can always use === and !==. This ensures that both data types and data values ​​are controlled in the existing program, which can avoid potential programming errors and bugs in the program.