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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:52:38+00:00 2026-05-24T02:52:38+00:00

In JavaScript undefined can be reassigned, so it is often advised to create a

  • 0

In JavaScript undefined can be reassigned, so it is often advised to create a self executing function that assures undefined is actually undefined. As an alternative null and undefined are definitely == but are any other values loosely equivalent to null/undefined?

TLDR

Basically can you safely replace this:

(function(undefined){

   window.f = function(obj){
     if(obj===undefined || obj===null ){
       alert('value is undefined or null');
     }
   }

})();

with:

window.f = function(obj){
  if(obj==null){
    alert('value is undefined or null');
  }
}

If the above is 100% safe, why doesn’t the JavaScript community/libraries drop undefined altogether and use the shorter x == null conditional to check for both null/undefined at once?

EDIT:

I have never seen someone actually represent an “unknown value” with ‘undefined’ vs null? I have never seen this scenario, and is why I originally asked the question. It just appears to be two incredibly confused values that are never used in their original intent. Standardizing everything to do a comparison obj==null would be beneficial for size and avoid any issues with reassignment. Everything would continue to work

var obj={};
obj.nonExistantProperty==null // true

var x;
ix==null // true

function(obj){
  obj==null // true
}

The one exception to this rule appears to be when casting undefined/null to an integer. This is a pretty age case scenario, but definitely should be noted.

+(null)==0
while
isNaN(+undefined)

Considering NaN is the only value in JavaScript not equal to itself, you can do some pretty crazy things like:

+undefined == +undefined // false
+null == +null // true

Using null as a loose equality == drop in replacement for undefined is safe, provided you don’t plan to cast the value to an integer. Which is a pretty edge case scenario.

  • 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-24T02:52:38+00:00Added an answer on May 24, 2026 at 2:52 am

    The abstract equality algorithm from section 11.9.3 of the language spec is what defined == and != and it defines them such that

    null == void 0
    null == null
    void 0 == null
    

    where void 0 is just a reliable way of saying undefined (see below) so the answer to your question is yes, null is equal to undefined and itself and nothing else.

    The relevant parts of the spec are

    1. If Type(x) is the same as Type(y), then
         If Type(x) is Undefined, return true.
         If Type(x) is Null, return true.
         ...
    2. If x is null and y is undefined, return true.
    3. If x is undefined and y is null, return true.
    ...
    

    If you’re worried about undefined meaning something other than what it normally means, use void 0 instead.

    null               ==  void 0           // True
    ({}).x             === void 0           // True
    "undefined"        === typeof void 0    // True
    (function () {})() === void 0           // True
    (undefined = 42,
     undefined         === void 0)          // False
    "undefined"        === typeof undefined // False
    "undefined"        === typeof void 0    // True
    

    From the language specification:

    11.4.2 The void Operator

    The production UnaryExpression : void UnaryExpression is evaluated as follows:

    1. Let expr be the result of evaluating UnaryExpression/.
    2. Call GetValue(expr).
    3. Return undefined.

    So the void prefix operator evaluates its argument and returns the special value undefined regardless of to what the global variable undefined has been changed (or whether undefined is defined :).

    EDIT: In response to comments,

    If you are dealing with library code that distinguishes between the two, then you need to deal with the difference. Some of the new libraries standardized by the language committee do ignore the difference : JSON.stringify([void 0]) === "[null]" but there is too much code out there that treats them subtly differently, and there are other differences :

    +(null) === 0
    isNaN(+undefined)
    
    "" + null === "null"
    "" + undefined === "undefined"
    

    If you’re writing any kinds of libraries that produce text or serialize/deserialize and you want to conflate the two then you can’t pass undefined through and expect it to behave as null — you need to explicitly normalize your inputs to one or the other.

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

Sidebar

Related Questions

I'm trying to create a javascript function that can take a fraction input string
How do I remove all attributes which are undefined or null in a JavaScript
I have a problem with a javascript error: $(#slider) is undefined How can i
I’m a bit confused about JavaScript’s undefined and null values. What does if (!testvar)
I often see JavaScript code where a function may take in an options object
I want to write a javascript function, that will make an ajax request to
How do I check if an object property in JavaScript is undefined?
JavaScript does funky automatic conversions with objects: var o = {toString: function() {return 40;
Javascript: The Good Parts is a great book. Often I find myself reading passages
In JavaScript, the this operator can refer to different things under different scenarios. Typically

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.