Comparisons Flashcards
a==b
write not equal to
a != b
let result = 5 > 4 // returns
true
alert( ‘Z’ > ‘A’ ); // returns
alert( ‘Bee’ > ‘Be’ ); // returns
true
true
alert( ‘Z’ > ‘Z’ ); // returns
false
alert( ‘Glow’ > ‘Glee’ ); // returns and why
true
o is greater than e.
A capital letter “A” is not equal to the lowercase “a”. Which one is greater and why?
The lowercase “a”. Why? Because the lowercase character has a greater index in the internal encoding table JavaScript uses (Unicode).
alert( ‘2’ > 1 ); // returns and why?
true, string ‘2’ becomes a number 2
JavaScript converts the values to numbers.
alert( ‘01’ == 1 ); // returns and why?
true, string ‘01’ becomes a number 1
JavaScript converts the values to numbers.
let a = 0;
let b = “0”;
alert(a == b); // returns and why
true
An equality check converts values using the numeric conversion (hence “0” becomes 0), while the explicit Boolean conversion uses another set of rules.
alert( 0 == false ); // returns
true
alert( 0 === false ); // returns and why
false
Because the types are different
alert( 0 !== false );
true
alert( null === undefined );
false
alert( null == undefined );
true
For maths and other comparisons < > <= >=
null is converted to ________
number
For maths and other comparisons < > <= >=
undefined becomes ________.
NAN
alert( null > 0 ); // returns
false
alert( null == 0 ); // returns and why?
false
the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don’t equal anything else. That’s why (2) null == 0 is false.
alert( null >= 0 ) // returns and why?
true
Comparisons convert null to a number, treating it as 0. That’s why (3) null >= 0 is true
alert( undefined > 0 ); //
false
alert( undefined < 0 ); // returns
false
alert( undefined == 0 ); // returns
false