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 7917211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:04:07+00:00 2026-06-03T15:04:07+00:00

Possible Duplicate: Conflicting boolean values of an empty JavaScript array What is the rationale

  • 0

Possible Duplicate:
Conflicting boolean values of an empty JavaScript array

What is the rationale behind the fact that

[ ([] == false), ([] ? 1 : 2) ]

returns [true, 1]?

In other words an empty list is logically true in a boolean context, but is equal to false.

I know that using === solves the issue, but what is the explanation behind this apparently totally illogical choice?

In other words is this considered a mistake in the language, something unintentional that just happened and that cannot be fixed because it’s too late or really in the design of the language someone thought it was cool to have this kind of apparent madness that I’m sure is quite confusing for many programmers?

The technical explanation of how this happens is at the same time amazing and scaring, I was however more interested in what is behind this design.

Edit

I accepted very detailed Nick Retallack explanation, even if it’s only about the technical reasons of why []==false is true: surprisingly enough it happens is because [] converted to string is an empty string and the empty string numeric value is special cased to be 0 instead of the apparently more logical NaN. With an empty object for example the comparison ({}) == false returns false because the string representation of an empty object is not the empty string.

My curiosity still remains about all this being just unanticipated (and now unfortunately solidified in a standard).

  • 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-03T15:04:08+00:00Added an answer on June 3, 2026 at 3:04 pm

    Let’s get technical. I’ll explain the logic with quotes from the ECMAScript Standard 262.

    The expression [] ? 1 : 2 is very simple:

    11.12 Conditional Operator ( ? : )

    • Let lref be the result of evaluating LogicalORExpression.
    • If ToBoolean(GetValue(lref)) is true, then
      • Let trueRef be the result of evaluating the first AssignmentExpression.
      • Return GetValue(trueRef).
    • Else
      • Let falseRef be the result of evaluating the second AssignmentExpression.
      • Return GetValue(falseRef)

    9.2 ToBoolean

    • Undefined: false
    • Null: false
    • Boolean: The result equals the input argument (no conversion).
    • Number: The result is false if the argument is +0, 0, or NaN; otherwise the result is true.
    • String: The result is false if the
      argument is the empty String (its length is zero); otherwise the
      result is true.
    • Object: true

    So it’s true.


    Now for the wild ride of what happens when you use the double equals operator. Perhaps this will help explain why you should never do this.

    The behavior of == is explained in in section 11.9.3: The Abstract Equality Comparison Algorithm.

    For x == y where x = [] and y = false, this happens:

    11.9.3: The Abstract Equality Comparison Algorithm

    If Type(y) is Boolean, return the result of the comparison x == ToNumber(y)

    9.3 ToNumber

    The result is +0 if the argument is
    false.

    Now we have [] == 0

    11.9.3: The Abstract Equality Comparison Algorithm

    If Type(x) is Object and Type(y) is either String or Number, return
    the result of the comparison ToPrimitive(x) == y.

    9.1 ToPrimitive

    Return a default value for the Object. The default value of an object
    is retrieved by calling the [[DefaultValue]] internal method of the
    object, passing the optional hint PreferredType. The behaviour of
    the [[DefaultValue]] internal method is defined by this specification
    for all native ECMAScript objects in 8.12.8.

    8.12.8 DefaultValue:

    When the [[DefaultValue]] internal method of O is called with no
    hint, then it behaves as if the hint were Number

    • Let valueOf be the result of calling the [[Get]] internal method of object O with argument “valueOf”.
    • If IsCallable(valueOf) is true then,
      • Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and an empty argument list.
      • If val is a primitive value, return val
    • Let toString be the result of calling the [[Get]] internal method of object O with argument “toString”.
    • If IsCallable(toString) is true then,
      • Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and an empty argument list.
      • If str is a primitive value, return str.

    I assume this first attempts valueOf and then rejects it because the result is the same array you started with. It then calls toString on Array, which seems to be universally implemented as a comma separated list of its values. For empty arrays like this one, that results in empty string.

    Now we have ” == 0

    11.9.3: The Abstract Equality Comparison Algorithm

    If Type(x) is String and Type(y) is Number, return the result of the
    comparison ToNumber(x) == y

    9.3.1 ToNumber Applied to the String Type

    A StringNumericLiteral that is empty or contains only white space is
    converted to +0.

    Now we have 0 == 0

    11.9.3: The Abstract Equality Comparison Algorithm

    If x is the same Number value as y, return true

    Awesome. It’s true. Pretty convoluted way of getting here though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Working with latitude/longitude values in Java Duplicate: Working with latitude/longitude values in
Possible Duplicate: Elements order - for (... in ...) loop in javascript Assume you
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the

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.