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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:20:15+00:00 2026-06-05T03:20:15+00:00

Take this snippet, var a = { } if(typeof a.c === ‘undefined’){ console.log(‘inside if’);

  • 0

Take this snippet,

var a = {

}

if(typeof a.c === 'undefined'){
 console.log('inside if');
}
if(a.c === undefined){
 console.log('inside if');
}

Both if results in true. Is there any difference in both statements specific to some browsers?

Also, in my last project I have already used typeof a.c == 'undefined' numerous times to check values in json data.

Now, I know that this is not good way, as some value may be undefined too, so my logic will fail.

I should have used hasOwnProperty .

But I am sure that no value will be undefined , can I use typeof a.c == 'undefined' in place of hasOwnProperty or should I change all my typeof with hasOwnProperty

  • 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-05T03:20:16+00:00Added an answer on June 5, 2026 at 3:20 am

    (UPDATE: You might want to check out this question: variable === undefined vs. typeof variable === "undefined").

    In very old browsers (Netscape 2, IIRC, and possibly IE 4 or lower), you couldn’t compare a value with undefined, since that caused an error. In any (semi-) modern browser, however, there’s no reason to check typeof value === 'undefined' instead of value === undefined (except for paranoia that somebody might have redefined the variable undefined).

    hasOwnProperty serves a different purpose. It checks if the object has a property with the given name, and not its prototype; i.e. regardless of the inherited properties. If you want to check whether an object contains a certain property, inherited or not, you should use if ('c' in a) {…

    But basically, these will probably all work:

    if (a.c === undefined) console.log('No c in a!');
    if (typeof a.c === 'undefined') console.log('No c in a!');
    if (!('c' in a)) console.log('No c in a!');
    if (!a.hasOwnProperty('c')) console.log('No c in a!');
    

    The main differences being that:

    • a.c === undefined will produce an unexpected result if someone has done undefined = 'defined' or some such trick;
    • !('c' in a) is not very readable (IMHO)
    • !a.hasOwnProperty('c') will return false if the object a doesn’t contain a property c, but its prototype does.

    Personally, I prefer the first since it’s more readable. If you’re paranoid and want to avoid the risk of a redefined undefined, wrap your code in a self-executing anonymous function as follows:

    (function (undefined) {
      // in here, 'undefined' is guaranteed to be undefined. :-)
    
      var a = {
    
      };
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at this snippet below: <script> $(function(){ var to_username = alex; window.Persona
Take a look at this snippet: var obj = { fn: function () {return
I have a very basic question. Lets take this snippet: #include <stdio.h> void foo(void)
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Take the following code snippet: <script> var x = 25; document.write(x << 9); </script>
Take this code snippet as example: union stack { int a; float b; };
Take a look at the snippet below. Is there any function I could write
Take this query: SELECT * FROM MyTable WHERE MyColumn = 'SomeValue' ORDER BY SomeFakeQualifier.MyColumn
Take this example code (ignore it being horribly inefficient for the moment) let listToString

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.