If you know the settings in advance you can define it in a single statement:

var defaultsettings = {
                        ajaxsettings : { "ak1" : "v1", "ak2" : "v2", etc. },
                        uisettings : { "ui1" : "v1", "ui22" : "v2", etc }
                      };

If you don't know the values in advance you can just define the top level object and then add properties:

var defaultsettings = { };
defaultsettings["ajaxsettings"] = {};
defaultsettings["ajaxsettings"]["somekey"] = "some value";

Or half-way between the two, define the top level with nested empty objects as properties and then add properties to those nested objects:

var defaultsettings = {
                        ajaxsettings : {  },
                        uisettings : {  }
                      };

defaultsettings["ajaxsettings"]["somekey"] = "some value";
defaultsettings["uisettings"]["somekey"] = "some value";

You can nest as deep as you like using the above techniques, and anywhere that you have a string literal in the square brackets you can use a variable:

var keyname = "ajaxsettings";
var defaultsettings = {};
defaultsettings[keyname] = {};
defaultsettings[keyname]["some key"] = "some value";

Note that you can not use variables for key names in the { } literal syntax.

Answer from nnnnnn on Stack Overflow
🌐
W3Schools
w3schools.com › js › tryit.asp
Nested JavaScript Objects and Arrays.
The W3Schools online code editor allows you to edit code and view the result in your browser
Discussions

I need help with JS Nested Objects...
Objects don't have access to their parents like how you might access a parent folder in your file system. Given any arbitrary object you can only dig down into the properties the object contains, not go up into any object that might contain it. One of the reasons for this is that an object might have multiple parents. Its perfectly allowable for one object to be assigned as a property of two different objects while still being the same object (not a copy). const obj = { nest: function() { console.log("what is my parent?"); } }; const parent1 = { child: obj }; const parent2 = { child: obj }; console.log(parent1.child === parent2.child); // true obj.nest(); // "what is my parent?" parent1.child.nest(); // "what is my parent?" parent2.child.nest(); // "what is my parent?" However, in your particular case, since you are defining createUser yourself, you have the option to make the object created accessible to any other function you created in there as well. All you need to do is assign it to a variable first before returning it. That way any function within the object will have access to the entire object structure using that variable name. From that you can dig down into any value you want. function createUser (name = 'default', age = 0) { const user = { name, age, profile: { city: "NYC", state: "New York", country: "US", greet: function() { console.log(name); console.log(this.city); }, obj: { nest: function() { console.log(name); console.log(user.profile.city); // <-- going down from the root, no way up } } } }; return user; } The only way to go up through a structure like this is if you provide the links yourself. If you've worked with the HTML DOM you may have seen an example of this. Each element there has a parentNode that lets you go up to the parent node of the DOM tree. What's different about the DOM vs regular JavaScript objects is that the DOM manages those parents for you whenever you add or move elements within the tree. And in the DOM you can only have one parent. If you have an element as a child of a div and add it to also be a child of a p, it gets automatically removed from the div parent so that the only parent is now the p. So while having something in place to let you easily walk up parents like that is possible, a lot more bookkeeping is involved - bookkeeping that isn't present in normal JavaScript objects. More on reddit.com
🌐 r/learnjavascript
4
3
June 13, 2025
Accessing nested objects like 2D arrays
So I’m not sure whether I have a fundamental misunderstanding of how objects work, but I’m trying to write a program which requires accessing nested objects, however it’s not a simple case of accessing the object via the object name. For example, if I have: const itemsList = { item1: ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
4
0
April 22, 2022
Accessing Nested Objects
Hello! So for the Javascript challenges, I am stuck in the “Accessing Nested Objects” part. The sub-properties of objects can be accessed by chaining together the dot or bracket notation. Here is a nested object: v… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
11
1
July 15, 2017
How do I build a nested object in Javascript dynamically?
Hi FCC, I’ve been struggling with this problem for a day or two and I have been able to get close but not the full solution that I am looking for. Say I have two arrays like so, const arr1 = ['a','b']; const arr2 = ['a… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
15
0
August 11, 2019
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Nested objects in real apps - Curriculum Help - The freeCodeCamp Forum
July 8, 2022 - In Modify an Object Nested Within an Object step we have this example: let nestedObject = { id: 28802695164, date: 'December 31, 2016', data: { totalUsers: 99, online: 80, onlineStatus: { active:…
🌐
CodeSignal
codesignal.com › learn › courses › mastering-task-decomposition-in-javascript › lessons › parsing-and-updating-nested-objects-in-javascript
Parsing and Updating Nested Objects in JavaScript
If the value part of a key-value pair contains another key-value string, it should be represented as a nested object. For example, the input string "A1=B1,C1={D1=E1,F1=G1},I1=J1" should be converted into the following nested JavaScript object:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-create-a-nested-object-in-javascript
How to Create a Nested Object in JavaScript ? - GeeksforGeeks
July 23, 2025 - JavaScript allows us to create objects having the properties of the other objects this process is called as nesting of objects.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object
Object - JavaScript | MDN
The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.
Find elsewhere
🌐
Medium
medium.com › @mcdonough.mollya › working-with-nested-objects-in-javascript-aad41ae5ed85
Working with Nested Objects in JavaScript | by Molly McDonough | Medium
March 4, 2022 - Working with Nested Objects in JavaScript Objects are JavaScript’s only non-primitive data type. They store key, value pairs. Keys are always strings, and the values can be any data type (numbers …
🌐
Reddit
reddit.com › r/learnjavascript › i need help with js nested objects...
r/learnjavascript on Reddit: I need help with JS Nested Objects...
June 13, 2025 -

Hi everyone, I’m currently learning JavaScript and working through the topic of objects (Nested Objects). I was wondering: how can a method inside a nested object access a property from its parent object?

For example, if I have an object inside another object, and the inner object wants to read a value defined in the outer one. How do I do that?

Thanks in advance! Here's the code:

function createUser (name = 'default', age = 0)
{
    return {
        name, age,
        profile:
        {
            city: "NYC",
            state: "New York",
            country: "US",
            //So I can access 'name' (since its a variable), & 'city' like this...
            greet: function() {console.log(name); console.log(this.city)},
            obj:
            {
                //I can still access 'name' (variable), but not 'city' (not using 'this' because 'this' refers to 'obj', & not anyways since it's not in the scope)...What do I do if I want to????
                nest: function() {console.log(name); console.log(city)}
            }
        }
    };
}

let userOne = createUser("john", 10);

userOne.profile.greet();
userOne.profile.obj.nest();

Top answer
1 of 1
6
Objects don't have access to their parents like how you might access a parent folder in your file system. Given any arbitrary object you can only dig down into the properties the object contains, not go up into any object that might contain it. One of the reasons for this is that an object might have multiple parents. Its perfectly allowable for one object to be assigned as a property of two different objects while still being the same object (not a copy). const obj = { nest: function() { console.log("what is my parent?"); } }; const parent1 = { child: obj }; const parent2 = { child: obj }; console.log(parent1.child === parent2.child); // true obj.nest(); // "what is my parent?" parent1.child.nest(); // "what is my parent?" parent2.child.nest(); // "what is my parent?" However, in your particular case, since you are defining createUser yourself, you have the option to make the object created accessible to any other function you created in there as well. All you need to do is assign it to a variable first before returning it. That way any function within the object will have access to the entire object structure using that variable name. From that you can dig down into any value you want. function createUser (name = 'default', age = 0) { const user = { name, age, profile: { city: "NYC", state: "New York", country: "US", greet: function() { console.log(name); console.log(this.city); }, obj: { nest: function() { console.log(name); console.log(user.profile.city); // <-- going down from the root, no way up } } } }; return user; } The only way to go up through a structure like this is if you provide the links yourself. If you've worked with the HTML DOM you may have seen an example of this. Each element there has a parentNode that lets you go up to the parent node of the DOM tree. What's different about the DOM vs regular JavaScript objects is that the DOM manages those parents for you whenever you add or move elements within the tree. And in the DOM you can only have one parent. If you have an element as a child of a div and add it to also be a child of a p, it gets automatically removed from the div parent so that the only parent is now the p. So while having something in place to let you easily walk up parents like that is possible, a lot more bookkeeping is involved - bookkeeping that isn't present in normal JavaScript objects.
🌐
Medium
medium.com › @Adekola_Olawale › handling-and-debugging-complex-nested-objects-in-javascript-9fb044d63669
Handling and Debugging Complex Nested Objects in JavaScript | by Adekola Olawale | Medium
November 14, 2025 - Think of it as JavaScript’s way of saying, “If it exists, keep going; otherwise, return undefined.” · Sometimes, you just want to know if a nested property exists before using it.
🌐
DEV Community
dev.to › ddrummer3993 › nested-object-iteration-using-multiple-forin-loops-4k6l
Nested object iteration using multiple for...in Loops. - DEV Community
March 23, 2022 - So we've successfully made it through the first level, but now how do we get to the next level of objects? That's right! another for...in loop! lets add a nested for...in loop to our function and console.log() the results:
🌐
DEV Community
dev.to › mlgvla › javascript-using-the-spread-operator-with-nested-objects-2e7l
JavaScript: Using the spread operator with nested objects - DEV Community
February 14, 2022 - Why? Objects in Javascript are passed by reference, not value. The top-level object and each nested object of newObject share the exact same locations in memory to those of object. Passing by reference means you are assigning the address location to newObject.
🌐
Medium
medium.com › data-scraper-tips-tricks › safely-read-write-in-deeply-nested-objects-js-a1d9ddd168c6
Safely Read & Write in Deeply Nested Objects in Javascript | by Gabin Desserprit | Data Hunter’s Blog | Medium
August 11, 2017 - As I play with datasets I oftenly make Classes to properly work with my Objects. I can then add nifty functions to each Class and share them all accross my project easily. Some time ago as I was playing with some pretty deeply nested objects I decided to make two ‘magic’ functions getValue() and setValue().
🌐
Zod
zod.dev › api
Defining schemas | Zod
To define a self-referential type, use a getter on the key. This lets JavaScript resolve the cyclical schema at runtime. const Category = z.object({ name: z.string(), get subcategories(){ return z.array(Category) } }); type Category = z.infer<typeof Category>; // { name: string; subcategories: Category[] }
🌐
SheCodes
shecodes.io › athena › 264237-how-to-nest-objects-in-javascript
[JavaScript] - How to nest objects in JavaScript - SheCodes | SheCodes
Learn how to nest objects within other objects in JavaScript and access nested object properties using dot notation.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Accessing nested objects like 2D arrays - JavaScript
April 22, 2022 - So I’m not sure whether I have a fundamental misunderstanding of how objects work, but I’m trying to write a program which requires accessing nested objects, however it’s not a simple case of accessing the object via the object name. For example, if I have: const itemsList = { item1: { ID: 0, itemName: "Item one", ... }, item2: { ID: 1, itemName: "Item two", ... }, ... } In my code, I want to access a particular item using a number which corresponds to the ID ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Accessing Nested Objects - JavaScript - The freeCodeCamp Forum
July 15, 2017 - So for the Javascript challenges, I am stuck in the “Accessing Nested Objects” part. The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
How do I build a nested object in Javascript dynamically? - Curriculum Help - The freeCodeCamp Forum
August 11, 2019 - Hi FCC, I’ve been struggling with this problem for a day or two and I have been able to get close but not the full solution that I am looking for. Say I have two arrays like so, const arr1 = ['a','b']; const arr2 = ['a.foo', 'b.bar']; The arrays will always be the same length.
🌐
Better Programming
betterprogramming.pub › 4-ways-to-safely-access-nested-objects-in-vanilla-javascript-8671d09348a8
4 Ways to Safely Access Nested Objects in Vanilla Javascript | by Zeng Hou Lim | Better Programming
September 10, 2019 - If you’re working with Javascript, chances are you might have encountered a situation where you have had to access a deeply nested object. If everything goes well, you get your data without any problems.