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

The Archive Base Latest Questions

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

I have some code that adds properties to an object like this: var MyObject

  • 0

I have some code that adds properties to an object like this:

var MyObject = new Object();

if (....) { MyObject.prop1 = .... ; }
if (....) { MyObject.prop2 = .... ; }
if (....) { MyObject.prop3 = .... ; }

After going through all these if statements, I want to test and see whether MyObject has properties. I don’t want to test if it has prop1 || prop2 || prop3 because there are 12 properties that can be added.

How do I test to see if it has at least one? I tried if (MyObject) but of course that doesn’t work because that only tests the existence of the object. Is these something like a simple one-liner like (MyArray.length) for objects?

  • 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-27T10:21:03+00:00Added an answer on May 27, 2026 at 10:21 am

    If you’re working in a pre-ECMAScript5 environment, it’s easy enough with a small loop:

    var found = false, name;
    for (name in MyObject) {
        if (MyObject.hasOwnProperty(name)) {
            found = true;
            break;
        }
    }
    

    Or since you’re using var MyObject = new Object(); to create it (BTW, you can just use var MyObject = {};), you don’t actually need the hasOwnProperty call:

    var found = false, name;
    for (name in MyObject) {
        found = true;
        break;
    }
    

    for..in loops through the enumerable properties of an object, both its own and ones it inherits from its prototype; hasOwnProperty tells you whether the property comes from the object itself or its prototype. All of the properties you’re adding will be enumerable, so they’ll show up in the loop. Raw objects ({}) have no enumerable properties initially unless someone has been mucking about with Object.prototype (which is a Really Bad Idea), hence the second loop above.

    ECMAScript5 (released a couple of years ago, not supported by older browsers and support varies a bit even in more recent ones) adds a couple of functions you could use for this. Probably the most relevant is Object.keys, which returns an array of the names of the object’s “own” properties (not properties it inherits from its prototype). That would mean you wouldn’t need the loop:

    if (Object.keys(MyObject).length) { // Only on ES5-compliant browsers
        // Yes it has at least one
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that effectively does this : File file = new File(C:\\Program
I have some code that outputs like this: void exec_prompt(char * usr_name, char *
I have some setup code in my functions.php file that sets permalinks and adds
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that adds to a std::vector and a std::map after creating
I have some code that adds mouseover events and mouseout events to all 'a'
CODE I have some code that adds a UILongPressGestureRecognizer gesture recognizer called _recognizer to
I have some code that adds a flag to an email but when I
I have have some code which adds new cells to a table and fills

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.