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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:16:40+00:00 2026-05-31T00:16:40+00:00

I usually find myself working with deep objects like this: var x = {

  • 0

I usually find myself working with deep objects like this:

var x = {
  y: {
    z: {
      a:true
    }
  }
}

And somewhere in the code:

if( x.y.z.a === true ){
  //do something
}

And in some cases any of the x,y,z variables could be undefined, in which case you would get “Cannot read property * of undefined“

Potential solution is:

if( x && x.y && x.y.z && x.y.z.a === true ){
  //do something
}

jsfiddle: http://jsfiddle.net/EcFLk/2/

But is there any easier/shorter way? Inline solutions (without using special function) would be great. Thanks.

  • 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-31T00:16:42+00:00Added an answer on May 31, 2026 at 12:16 am

    Nope, you’ve already found the right way. Of course, you can use a try/catch block and handle the error after-the-fact, but I’d use the x && x.y && x.y.z && x.y.z.a solution.

    (You don’t need the === true unless you really want the condition to only be true when a is strictly equal to true and not when it’s 1 or "hi", but from your question, I’m thinking you know that already.)


    You’ve said you don’t want to use a function for this, and I haven’t felt the need for one either, but just for fits and giggles:

    function ref(obj, names) {
        var rv = obj, index;
        if (names) {
            for (index = 0; rv && index < names.length; ++index) {
                rv = rv[names[index]];
            }
        }
        return rv;
    }
    

    Usage:

    if (ref(x, ["y", "z", "a"]) === true) {
        // do something
    }
    

    Function calls are so cheap these days…

    Or alternately:

    function ref(obj) {
        var rv = obj, index;
        for (index = 1; rv && index < arguments.length; ++index) {
            rv = rv[arguments[index]];
        }
        return rv;
    }
    

    Usage:

    if (ref(x, "y", "z", "a") === true) {
        // do something
    }
    

    …but on most JavaScript engines, that will be slower (arguments tends to be slow). But then again, you’d have to be doing it thousands of times in a loop for the speed to be an issue.

    Or as Šime suggests, a single variable (I was avoiding the split, but it’s not expensive):

    function ref(obj, path) {
        var rv = obj, names = path.split("."), index;
        for (index = 0; rv && index < names.length; ++index) {
            rv = rv[names[index]];
        }
        return rv;
    }
    

    Usage:

    if (ref(x, "y.z.a") === true) {
        // do something
    }
    

    Live example of all three | Live source

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

Sidebar

Related Questions

Usually when I need to fork in C, I do something like this: pid_t
I find myself revisit areas of my project and refine them again. This usually
I'm planning to make a simple Photo Resizer since I usually find myself having
I'm trying to understand a particular Perl code from vcake . Usually I find
Sometimes I need to find some strings inside DB usually it is just host-name
I usually play with elisp code on my scratch buffer. I find it hard
I have had to do this several times, usually when trying to find in
I don't use DataSets much. Usually find myself using an ORM or just a
I'm an ASP.NET developer, and I usually find myself leaving the webpage that I'm
I work with jQuery+Spring+Hibernate stack a lot. Usually I find myself developing complex interfaces

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.