0

Can you explain how works the Comparison Operators in JS:

 "a" > "A" // => why true?
  null == undefined; // and here as well?

and some others

   null >  0;
   null >= 0;

1 Answer 1

2

Strings are compared by their character codes, ie. their positions in the Unicode table.
A is 65, a is 97. Therefore "a" > "A".

== is a loose comparison. null == undefined is a special case, since the abstract equality comparison algorithm explicitly states that true should be returned when comparing these two values:

2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.

null > 0 is false, and null >= 0 is true because null, when converted to a number, is zero.

Sign up to request clarification or add additional context in comments.

8 Comments

null and undefined are not converted when doing null == undefined. The abstract equality algorithm explicitly states that true should be retuned in that case.
but: null === 0 will be false?
@user2754035: You can just type it in the console and find out. But yes, it's false since null and 0 are of different data types. If you want to know what happens when you compare two values with === or ==, have a look at the specification: es5.github.io/#x11.9.3 .
@Tyblitz Sadly, +rep means nothing here :p I already have my +200 for the day. Thanks for the upvote though!
Are you going to fix the part about null and undefined?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.