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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:31:21+00:00 2026-05-27T22:31:21+00:00

I have a problem with a very simple piece of code written in Javascript,

  • 0

I have a problem with a very simple piece of code written in Javascript, could you help me please?

Here’s what I think I have understand so far about javascript and variables:

  • An undefined value is evaluated to false in a boolean operation
  • By using a == operator in a comparation, you’re asking if two values are comparable regardless of their types

I found an exercise file in a online course and I tried to do it, but I didn’t got the same result expected in the lesson; the main problem was that I was comparing the value through a “if value == false { … }” while the solution was using a “if !value { … }”

So I decided to write a very short code in order to try it by myself, but I’m getting mixed results. Here in the example below I would expect this JS code to generate two identical alerts (“foo is equal to false”), but instead the first if statement returns “foo IS NOT equal to false” while the second if returns (as expected) “foo is equal to false”.

This is what I written:

var foo = undefined;

if (foo == false) {
  alert("foo is equal to false");
} else {
  alert("foo is not equal to false"); // Javascript executes this row
}

if (!foo) {
  alert("foo is equal to false");  // Javascript executes this row
} else {
  alert("foo is not equal to false");
}

AFAIK the two IFs should do the same work, and infact when I tried it by replacing in the first line the value “var foo = undefined;” with “var foo = 0;” it worked as expected, and 0 is another value that should be evaluated to false, or at least this is what I remember.

Could you tell me what I’m doing wrong?

  • 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-05-27T22:31:22+00:00Added an answer on May 27, 2026 at 10:31 pm

    The == algorithm (Abstract Equality Comparison Algorithm) isn’t something where you can simply assume an outcome unless you know the algorithm. You need to know the details of how it works.

    For example, null and undefined are a special case. They do not do any type conversion other than to be considered equal to each other.

    Otherwise there’s typically a type conversion that tries to reduce both operands to a common type. This often ends up being a toNumber conversion.

    That’s why:

    • null == undefined; // true

    • null == 0; // false

    • +null == '0' // true

    So if you know how the algorithm works, you know that undefined never equals anything except for undefined and null, but other types that are not strictly equal may be coerced down to types that are equal.

    So doing if(!x) vs if(x==false) are entirely different tests.

    • if(!x) performs toBoolean conversion.

    • if(x == false) uses a complex algorithm to decide the proper conversion.

    So with…

    if(x == false)
    

    …if x is undefined, it is determined to not be equal to false, yet if x is 0 or even "0", it will be considered equal to false.

    • 0 == false; // true

    • "0" == false; // true

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

Sidebar

Related Questions

I have a very simple problem. I have an application which is written in
I have a very simple piece of code (just for testing): import smtplib import
I have - I think - a very simple problem I want to align
i have very simple problem. I need to create model, that represent element of
I have a very simple problem which requires a very quick and simple solution
I have a very simple problem and a solution that will work, but I'm
I have a very simple problem but cannot find a nice solution. I have
I have made a very simple AppleScript to close tabs in Safari. The problem
have small problem, and would very much appreciate help :) I should convert byte
Very simple problem: I have a Public Sub (in a module) that I want

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.