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

  • Home
  • SEARCH
  • 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 8461635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:55:59+00:00 2026-06-10T13:55:59+00:00

Can someone explain why in javascript, alert({} == true) shows false if ({}) alert(‘true’)

  • 0

Can someone explain why in javascript,

alert({} == true) shows false

if ({}) alert('true') shows true ?

What’s different in the if condition that changes the result?

I wanted to write some shorthand argument validator obj || (obj = {}); and I was baffled by this discovery.

  • 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-10T13:56:00+00:00Added an answer on June 10, 2026 at 1:56 pm

    if ({}) alert('true') -> true

    {} is an object, which, when evaluated in the context of an if statement, gets coerced to a Boolean, and since Boolean({}) evaluates to true, you get if (true). This is documented in the ECMAScript specification, Section 12.5 The if Statement:

    The production If Statement : if ( Expression ) Statement is evaluated
    as follows:

    1. Let exprRef be the result of evaluating Expression.
    2. If ToBoolean(GetValue(exprRef)) is false, return (normal, empty, empty).
    3. Return the result of evaluating Statement.

    alert({} == true) -> false

    This one is more tricky. From the ECMAScript specification, Section 11.9.3 The Abstract Equality Comparison Algorithm:

    The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

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

    Thus, {} == true will be evaluated as {} == Number(true), which is evaluated to {} == 1, which is false.

    This is also why 1 == true evaluates to true, but 2 == true evaluates to false.

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

Sidebar

Related Questions

I've noticed an unusual behavior when developing using javascript, can someone explain that to
Can someone explain how JavaScript color pickers work? (The ones that don't use HTML5
can someone explain me why this script is not working? <script type=text/javascript> function destroy(ID)
Can someone explain how the organisation of classes in Pharo works in different versions
Can someone explain to me why I was running across a race condition with
I'm hoping someone can explain to me why the below JavaScript/HTML will show door
Can someone explain this OOP javascript structure? I realize it is how you create
Can someone please explain why this JavaScript code outputs zero instead of one? Also,
Possible Duplicate: Javascript parseInt gives very unexpected results Can someone explain why javascript interprets
I am new to JSON and JavaScript objects. Can someone please explain the differences

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.