Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:55:37+00:00 2026-06-18T08:55:37+00:00

What rules apply for the JavaScript relational comparison operators when the operands are of

  • 0

What rules apply for the JavaScript relational comparison operators when the operands are of different types?

For example, how is true > null evaluated? I can type this into my developer console and it gives the result true, but why?

I searched for a bit, but didn’t find any blog posts explaining this, although there are plenty explaining type coercion for == and === comparison operators.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:55:38+00:00Added an answer on June 18, 2026 at 8:55 am

    JavaScript relational comparison operator type coercion is defined in the JavaScript specification, specifically in sections 11.8 to 11.8.5 which describe the operators, and sections 9.1 (ToPrimitive) and 9.3 (ToNumber) which describe the process of coercing the operands.

    In short, the 4 comparison operators (<, >, <=, and >=) do their best to convert each operand to a number, then compare the numbers. The exception is when both operands are strings, in which case they are compared alphabetically.

    Specifically,

    1. If an argument o is an object instead of a primitive, try to convert it to a primitive value by calling o.valueOf() or – if o.valueOf wasn’t defined or didn’t return a primitive type when called – by calling o.toString()

    2. If both arguments are Strings, compare them according to their lexicographical ordering. For example, this means "a" < "b" and "a" < "aa" both return true.

    3. Otherwise, convert each primitive to a number, which means:

      • undefined -> NaN
      • Null -> +0
      • Boolean primitive type -> 1 if true, +0 if false
      • String -> try to parse a number from the string
    4. Then compare each item as you’d expect for the operator, with the caveat that any comparison involving NaN evaluates to false.

    So, this means the following:

    console.log(true > null);           //prints true
    console.log(true > false);          //prints true
    console.log("1000.0" > 999);        //prints true
    console.log("  1000\t\n" < 1001);   //prints true
    
    var oVal1 = { valueOf: function() { return 1; } };
    var oVal0 = { toString: function() { return "0"; } };
    
    console.log(oVal1 > null);         //prints true
    console.log(oVal0 < true);         //prints true
    console.log(oVal0 < oVal1);        //prints true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The rules for promotion is when operands are of different types, automatic binary numeric
I would like to apply transition rules to elements with javascript, so far this
I have a registration form where this CSS rules apply. when user submits with
Can someone explain the rules of casting, and when a conversion is ambiguous? I'm
Let f x y = x * y . We can apply this function
Is there a way to make some CSS rules apply only for Opera (11)?
When passing objects into functions, do the same rules apply to smart pointers as
while i know that xss rules should apply to iframed content if the domain,
I am trying to apply css rules to tr elements depending on what class
My problem is that all JavaScript that I apply to an ajax-loaded JqueryUI Dialog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.