How to get the Last Nested Property Using JavaScript ? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report The last nested property in JavaScript is the innermost property within a hierarchy of nested objects, which can be accessed by traversing through the object. These are the following ways to get the last nested property using JavaScript: Table of Content Using recursionUsing Array.is() methodMethod 1: Using recursionThe code iterates through each property of an object recursively. If a property's value is an object, it continues deeper, otherwise, it stores the property-value pair. It returns the last encountered property. Example: This example shows the accessing of the last nested property using recursion. JavaScript function nestedProperty(obj) { for (const key in obj) { if (typeof obj[key] === 'object') { return nestedProperty(obj[key]); } else { return { [key]: obj[key] }; } } } const obj = { a: { b: { c: 'Geek' } } }; const lastNestedProperty = nestedProperty(obj); console.log(lastNestedProperty); Output{ c: 'Geek' } Method 2: Using Array.is() methodThe function iterates through nested properties of an object until it reaches the last property. It checks if the current property is an object and retrieves its keys. Then, it traverses to the last property, ultimately returning the value of the last nested property. Example: This example shows the accessing of the last nested property using Arrray.is() method. JavaScript function nestedProperty(obj) { let result = obj; while (typeof result === 'object' && !Array.isArray(result)) { const keys = Object.keys(result); // Exit if object has no keys if (keys.length === 0) break; // Traverse to the last property result = result[keys[keys.length - 1]]; } return result; } // Example const obj = { a: { b: { c: 'Geeks' } } }; const lastNestedProperty = nestedProperty(obj); console.log(lastNestedProperty); OutputGeeks Comment More infoAdvertise with us Next Article How to get the Last Nested Property Using JavaScript ? G geekcoder2298 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to get the last item of JavaScript object ? In this article, we will learn how to get the last item of a Javascript object. Given a JavaScript object and the task is to get the last element of the JavaScript object. This can be done by the following methods: Using Object.keys() methodUsing for loopmethodApproach 1: Use Object.keys() method to 2 min read How to Get all Property Values of a JavaScript Object without knowing the Keys? To get all property values from a JavaScript object without knowing the keys involves accessing the object's properties and extracting their values.Below are the approaches to get all property values of a JavaScript Object:Table of ContentUsing Object.values() MethodUsing Object.keys() methodApproac 2 min read How to get the child element of a parent using JavaScript ? Given an HTML document, the task is to select a particular element and get all the child elements of the parent element with the help of JavaScript. Below are the approaches to get the child element of a parent using JavaScript:Table of ContentBy using the children propertyBy using the querySelector 3 min read How to add a property to a JavaScript object using a variable as the name? In JavaScript, you can dynamically add a property to an object using a variable as the name. This allows you to create flexible and dynamic objects based on runtime values, enhancing code reusability and adaptability. There are several methods that can be used to add a property to a JavaScript objec 2 min read How to filter nested JSON object to return certain value using JavaScript ? Given a collection that contains the detail information of employees who are working in the organization. We need to find some values from that nested collections of details. That collection is known as the JSON object and the information inside object are known as nested JSON object. Example 1: We 4 min read Like