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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:12:07+00:00 2026-05-24T23:12:07+00:00

I have seen two ways of detecting whether a UA implements a specific JS

  • 0

I have seen two ways of detecting whether a UA implements a specific JS property: if(object.property) and if('property' in object).

I would like to hear opinions on which is better, and most importantly, why. Is one unequivocally better than the other? Are there more than just these two ways to do object property detection? Please cover browser support, pitfalls, execution speed, and such like, rather than aesthetics.

Edit: Readers are encouraged to run the tests at jsperf.com/object-detection

  • 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-24T23:12:08+00:00Added an answer on May 24, 2026 at 11:12 pm
    • if(object.property)

      will fail in cases it is not set (which is what you want), and in cases it has been set to some falsey value, e.g. undefined, null, 0 etc (which is not what you want).

      var object = {property: 0};
      if(object.isNotSet) { ... } // will not run
      if(object.property) { ... } // will not run
      
    • if('property' in object)

      is slightly better, since it will actually return whether the object really has the property, not just by looking at its value.

      var object = {property: 0};
      if('property' in object) { ... } // will run
      if('toString' in object) { ... } // will also run; from prototype
      
    • if(object.hasOwnProperty('property'))

      is even better, since it will allow you to distinguish between instance properties and prototype properties.

      var object = {property: 0};
      if(object.hasOwnProperty('property')) { ... } // will run
      if(object.hasOwnProperty('toString')) { ... } // will not run
      

    I would say performance is not that big of an issue here, unless you’re checking thousands of time a second but in that case you should consider another code structure. All of these functions/syntaxes are supported by recent browsers, hasOwnProperty has been around for a long time, too.


    Edit: You can also make a general function to check for existence of a property by passing anything (even things that are not objects) as an object like this:

    function has(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    }
    

    Now this works:

    has(window, 'setTimeout'); // true
    

    even if window.hasOwnProperty === undefined (which is the case in IE version 8 or lower).

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

Sidebar

Related Questions

In my experience in object oriented C programming I have seen two ways to
I have seen the following two ways of specifying properties for an object literal
I have seen at least two ways to include an external log4net config file
I have seen programs exporting to Excel in two different ways. Opening Excel and
I have seen two different ways of merging local branches. git checkout master git
I have seen that there are two different ways to access methods within a
I have seen resources show two ways of allocating memory while ensuring that there
I have seen at least two ways: 1)to handle the touch screen events 2)to
I have seen two ways of acquiring and disposing resources. Either: Resource resource =
I would like to have a property that is always bound to the currently

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.