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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:30:00+00:00 2026-06-10T15:30:00+00:00

According to What is the difference between null and undefined in JavaScript? , null

  • 0

According to What is the difference between null and undefined in JavaScript?, null and undefined are two different objects (having different types) in Javascript. But when I try this code

var a=null;
var b;
alert(a==null);      // expecting true
alert(a==undefined); // expecting false
alert(b==null);      // expecting false
alert(b==undefined); // expecting true

The output of the above code is:

true
true
true 
true

Now as == only matches the value, I thought that both undefined and null must have the same value. So I tried:

alert(null) -> gives null

alert(undefined) -> gives undefined

I don’t understand how is this possible.

Here is the demo.

Edit

I understand that === will give the expected result because undefined and null have different types, but how does type conversion work in Javascript in the case of ==? Can we do explicit type conversion like we do in Java? I would like to apply a manual type conversion on undefined and null.

  • 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-10T15:30:02+00:00Added an answer on June 10, 2026 at 3:30 pm

    You need to use the identity operator ===, not the equality operator ==. With this change, your code works as expected:

    alert(a===null);      // true
    alert(a===undefined); // false
    alert(b===null);      // false
    alert(b===undefined); // true
    

    The reason the equality operator fails in this case is because it attempts to do a type conversion. undefined is of type undefined, and null is of type object; in attempting to compare the two, Javascript converts both to false, which is why it ends up considering them equal. On the other hand, the identity operator doesn’t do a type conversion, and requires the types to be equal to conclude equality.

    Edit Thanks to @user1600680 for pointing out, the above isn’t quite correct; the ECMAScript specification defines the null-to-undefined as special case, and equal. There’s no intermediate conversion to false.


    A simpler example of type conversion is number-to-string:

    console.log(5 == "5");    // true
    console.log(5 === "5");   // false
    

    The above answer has a good quote from Douglas Crockford’s Javascript: The Good Parts:

    [The “==” operator does] the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable.

    If you don’t believe that the rules are complicated and unmemorable, a quick look at those rules
    will disabuse you of that notion.

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

Sidebar

Related Questions

I have a div which has some text, but according to different situations this
According to the answers in this comparison question , there's no technical difference between
according to rails' built-in validation functions, what is the difference between these following two
I want to define different parameter according to different device, but I got a
I try to load files in different categories according to theis file name. Till
I was wondering the difference between these two dispatchEvent method... //1. eventObj:YouTubeSearchEvent = new
Can someone tell me what is the main difference between a JavaScript object defined
According to this article it should be a Javascript 2.0 way to define class.
According to the R language definition , the difference between & and && (correspondingly
What is the difference between these commands? Why does the first match, but grepping

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.