Hey, friends👋🏼
I am Aryan Sharma and I hope you all are doing great!
This is another blog in the JavaScript course for absolute beginners.
✍🏻I hope this blog course will help you become good at JavaScript.
So, I am starting this blog. Let's get started!!
Comparisons
From School days, we have learned about Comparison operators in Maths
In JavaScript, they are written as
Greater/less than:
a > b
,a < b
.Greater/less than or equals:
a >= b
,a <= b
.Equals:
a == b
, please note the double equality sign==
means the equality test, while a single onea = b
means an assignment.Not equals: In maths the notation is
â‰
, but in JavaScript it’s written asa != b
.
In this tutorial, we will learn more about different sorts of comparisons, how JavaScript performs them, and crucial differences.
At conclusion, you'll get an excellent recipe for avoiding "JavaScript oddities" troubles.
The outcome is Boolean
All comparison operators return a boolean value:
true
– means “yes” or “truth”.false
– means “no" or “not truth”.
let value = 5 > 4 ;
alert( value ) // true
let valiue2 = 5 < 4 ;
alert( value2 ) // false
String comparison
JavaScript uses the so-called "dictionary" or "lexicographical" order to determine whether one string is greater than another.
To put it another way, strings are compared letter by letter.
alert( 'B' > 'A' ); // true
alert( 'Aakash' > 'Akash' ); // true
How do we compare Strings :-
Take a look at the first character of both strings.
If the first character in the first string is larger (or less) than the first character in the second string, the first string is greater (or less). We've finished.
Otherwise, if the initial characters of both strings are the same, compare the second characters in the same way.
Repeat until one of the strings is exhausted.
Both strings are equal if they conclude at the same length. Otherwise, the longer string wins.
Comparison of different types
When comparing values of different data types, JavaScript converts them to numbers.
alert( '3' > 2 ); // true, string '3' becomes a number 2
alert( '11' == 3 ); // true, string '11' becomes a number 3
true
becomes 1
and false
becomes 0
for boolean values.
Comparison with null and undefined
There is an unusual behavior when null or undefined values are compared to other values.
For a strict equality check ===
These values differ because they are of different types.
alert( null === undefined ); // false
For a non-strict check ==
There’s a special rule. These two are a “sweet couple”: they equal each other (in the sense of ==
), but not any other value.
alert( null == undefined ); // true
An incomparable undefined
The value undefined
shouldn’t be compared to other values:
alert( undefined > 0 ); // false
alert( undefined < 0 ); // false
alert( undefined == 0 ); // false
Why does it dislike zero so much? Always false!
We get these results because:
Comparisons
(1)
and(2)
returnfalse
becauseundefined
gets converted toNaN
andNaN
is a special numeric value that returnsfalse
for all comparisons.The equality check
(3)
returnsfalse
becauseundefined
only equalsnull
,undefined
, and no other value.
âś…Tasks
What will be the result of these expressions?
5 > 4
"apple" > "pineapple"
"2" > "12"
undefined == null
undefined === null
null == "\n0\n"
null === +"\n0\n"
email me aryansh0004@gmail.com
**for answers
Hope you loved this blog!!❤️‍🔥
Please Like and Follow this blog! Have a wonderful day**