➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Comparison Operators in JavaScript | We will learn about numerous comparison operators and how they are implemented in Javascript in this tutorial. The logical processes that establish equality or difference between the values are often carried out using comparison operators.
Operators are accustomed to carrying out certain logical and mathematical operations on operands. JavaScript provides comparison operations just like C, C++, Java, Python, and many other languages. To assess if variables or values in logical expressions are equal or different, comparison operators are utilized.
Introduction to Comparison Operators in JavaScript
You employ a comparison operator to compare two values. The JavaScript comparison operators are displayed in the following table:-
Operator | Meaning |
---|---|
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
== | Equal to |
!= | Not equal to |
The outcome of a comparison operator is a Boolean result that indicates whether the comparison is accurate or not. See the example below:-
let r1 = 30 > 10;
console.log(r1); // true
let r2 = 30 < 10;
console.log(r2); // false
let r3 = 10 == 10;
console.log(r3); // true
Output:-
true
false
true
An operator for comparison requires two values. If there is an incompatibility between the types of values, the comparison operator turns them into values of comparable types in accordance with predetermined constraints.
Compare Numbers
The comparison operators carry out a numeric comparison if values are numerical data. For instance:-
let a = 100, b = 50;
console.log(b >= a);
console.log(a == 100);
Output:-
false
true
Compare Strings
JavaScript compares each character code in a string, one by one, if the operands are strings.
let name1 = 'sid', name2 = 'rick';
let result = name1 < name2;
console.log(result);
console.log(name1 == 'sid');
Output:-
false
true
Compare a Number to a Different Type of Value
The comparison operator will turn the non-numeric value into a number and compare the two values numerically if one value is a number and the other is not. For instance:-
console.log(10 < '30'); // true
Output:-
true
The comparison operator in this example turns the string “30” into the number 30 and compares it to the number 10. Here’s an illustration:-
console.log(10 == '10'); // true
Output:-
true
The comparison operator transforms the string “10” into the number 10 in this example and compares the two numbers numerically.
Compare an Object With a Non-Object
The function valueOf() { [native code] }() method of an object is invoked to return the value for comparison if a value is an object. The function toString() { [native code] }() function is used in place of the function valueOf() { [native code] }() method if the object doesn’t have one. For instance:-
let red = {
valueOf: function () {
return 10;
},
};
let orange = {
toString: function () {
return '20';
},
};
console.log(red > 10);
console.log(orange == 20);
Output:-
false
true
Boolean Comparison with Another Value
JavaScript translates a Boolean value to a number if it is a Boolean value and compares it to the other value. True is translated to 1 and false is transformed to 0. For instance:-
console.log(true > 0); // true
console.log(false < 1); // true
console.log(true > false); // true
console.log(false > true); // false
console.log(true >= true); // true
console.log(true <= true); // true
console.log(false <= false); // true
console.log(false >= false); // true
Output:-
true
true
true
false
true
true
true
true
Compare Null and Undefined
JavaScript defines null as undefined. The following equation yields true, which signifies that.
console.log(null == undefined); // true
Output:-
true
Compare NaN with Other Values
The equal operator (==) returns false if either value is NaN.
console.log(NaN == 1); // false
Output:-
false
Strict Equal (===) and Not Strict Equal (!==)
JavaScript also offers the strict equal (===) and not strict equal (!==) operators in addition to the comparison operators mentioned above.
Operator | Description |
---|---|
=== | Strict equal |
!== | Not strict equal |
With the exception of not converting the operand prior to comparison, the strict equal and non-strict equal operators act similarly to the equal and not equal operators. See the example below:-
console.log("20" == 20); // true
console.log("20" === 20); // false
Output:-
true
false
Since the equality operator is used in the initial comparison, JavaScript changes the string into a number and does the comparison.
However, because JavaScript doesn’t transform the string before comparison when we use the strict equal operator (===) in the second comparison, the outcome is false.
You have learned how to compare values using the JavaScript comparison operators in this article.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!