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

The Archive Base Latest Questions

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

If I have a reference to an object: var test = {}; that will

  • 0

If I have a reference to an object:

var test = {};

that will potentially (but not immediately) have nested objects, something like:

{level1: {level2: {level3: "level3"}}};

What is the best way to check for the existence of property in deeply nested objects?

alert(test.level1); yields undefined, but alert(test.level1.level2.level3); fails.

I’m currently doing something like this:

if(test.level1 && test.level1.level2 && test.level1.level2.level3) {
    alert(test.level1.level2.level3);
}

but I was wondering if there’s a better way.

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

    You have to do it step by step if you don’t want a TypeError because if one of the members is null or undefined, and you try to access a member, an exception will be thrown.

    You can either simply catch the exception, or make a function to test the existence of multiple levels, something like this:

    function checkNested(obj /*, level1, level2, ... levelN*/) {
      var args = Array.prototype.slice.call(arguments, 1);
    
      for (var i = 0; i < args.length; i++) {
        if (!obj || !obj.hasOwnProperty(args[i])) {
          return false;
        }
        obj = obj[args[i]];
      }
      return true;
    }
    
    var test = {level1:{level2:{level3:'level3'}} };
    
    checkNested(test, 'level1', 'level2', 'level3'); // true
    checkNested(test, 'level1', 'level2', 'foo'); // false
    

    ES6 UPDATE:

    Here is a shorter version of the original function, using ES6 features and recursion (it’s also in proper tail call form):

    function checkNested(obj, level,  ...rest) {
      if (obj === undefined) return false
      if (rest.length == 0 && obj.hasOwnProperty(level)) return true
      return checkNested(obj[level], ...rest)
    }
    

    However, if you want to get the value of a nested property and not only check its existence, here is a simple one-line function:

    function getNested(obj, ...args) {
      return args.reduce((obj, level) => obj && obj[level], obj)
    }
    
    const test = { level1:{ level2:{ level3:'level3'} } };
    console.log(getNested(test, 'level1', 'level2', 'level3')); // 'level3'
    console.log(getNested(test, 'level1', 'level2', 'level3', 'length')); // 6
    console.log(getNested(test, 'level1', 'level2', 'foo')); // undefined
    console.log(getNested(test, 'a', 'b')); // undefined

    The above function allows you to get the value of nested properties, otherwise will return undefined.

    UPDATE 2019-10-17:

    The optional chaining proposal reached Stage 3 on the ECMAScript committee process, this will allow you to safely access deeply nested properties, by using the token ?., the new optional chaining operator:

    const value = obj?.level1?.level2?.level3 
    

    If any of the levels accessed is null or undefined the expression will resolve to undefined by itself.

    The proposal also allows you to handle method calls safely:

    obj?.level1?.method();
    

    The above expression will produce undefined if obj, obj.level1, or obj.level1.method are null or undefined, otherwise it will call the function.

    You can start playing with this feature with Babel using the optional chaining plugin.

    Since Babel 7.8.0, ES2020 is supported by default

    Check this example on the Babel REPL.

    UPDATE: December 2019

    The optional chaining proposal finally reached Stage 4 in the December 2019 meeting of the TC39 committee. This means this feature will be part of the ECMAScript 2020 Standard.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I am assuming nhibernate can handle circular reference issues as I have not seen
I didn't have a understanding on difference between intializing a variable with {} and
i have a coode that are trying to get the id of xml file
I have a memory leak issue in my application which loads a large amount
I've searched stackoverflow and googled four a couple of hours and still not found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.