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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:26:23+00:00 2026-05-24T05:26:23+00:00

I have to check deeply-nested object property such as YAHOO.Foo.Bar.xyz . The code I’m

  • 0

I have to check deeply-nested object property such as YAHOO.Foo.Bar.xyz.

The code I’m currently using is

if (YAHOO && YAHOO.Foo && YAHOO.Foo.Bar && YAHOO.Foo.Bar.xyz) {
    // operate on YAHOO.Foo.Bar.xyz
}

This works, but looks clumsy.

Is there any better way to check such deeply nested property?

  • 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-24T05:26:25+00:00Added an answer on May 24, 2026 at 5:26 am

    If you expect YAHOO.Foo.Bar to be a valid object, but want to make your code bulletproof just in case it isn’t, then it can be cleanest to just put a try catch around it and let one error handler catch any missing segment. Then, you can just use one if condition instead of four that will detect if the terminal property exists and a catch handler to catch things if the intermediate objects don’t exist:

    try {
        if (YAHOO.Foo.Bar.xyz) {
            // operate on YAHOO.Foo.Bar.xyz
    } catch(e) {
        // handle error here
    }
    

    or, depending upon how your code works, it might even just be this:

    try {
        // operate on YAHOO.Foo.Bar.xyz
    } catch(e) {
        // do whatever you want to do when YAHOO.Foo.Bar.xyz doesn't exist
    }
    

    I particularly use these when dealing with foreign input that is supposed to be of a particular format, but invalid input is a possibility that I want to catch and handle myself rather than just letting an exception propagate upwards.

    In general, some javascript developers under-use try/catch. I find that I can sometimes replace 5-10 if statements checking input with a single try/catch around a larger function block and make the code a lot simpler and more readable at the same time. Obviously, when this is appropriate depends upon the particular code, but it’s definitely worth considering.

    FYI, if the usual operation is to not throw an exception with the try/catch, it can be a lot faster than a bunch of if statements too.


    If you don’t want to use the exception handler, you can create a function to test any arbitrary path for you:

    function checkPath(base, path) {
        var current = base;
        var components = path.split(".");
        for (var i = 0; i < components.length; i++) {
            if ((typeof current !== "object") || (!current.hasOwnProperty(components[i]))) {
                return false;
            }
            current = current[components[i]];
        }
        return true;
    }
    

    Example usage:

    var a = {b: {c: {d: 5}}};
    if (checkPath(a, "b.c.d")) {
        // a.b.c.d exists and can be safely accessed
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to create the hadoop-0.21.0-core.jar using the source code? I have check out the
I have to check some code and run it. I have the URL: svn+ssh://myuser@www.myclient.com/home/svn/project/trunk
We currently have check printing fully implemented and in the field for a POS
INSERT MODE i have check box in one tr. and another dropdown control in
I have to check a number if it satisfies the following criteria: in binary,
I have a check box, RequiredFieldValidator and textbox that have editmask extender I want
I have to check memory performance of my application, I have solved Leaks now
I have a check box and I have subscribed for the CheckedChanged event. The
I have to check if a value matches a certain string, and the input
I have to check connection leak in my application i.e. open connections which have

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.