'Is there a maximum depth level for nested objects in Javascript?

Javascript has no problems with nesting an object within itself, i.e. if you have an object A, you can pass it as a property on object A:

const nested = {};
nested.name = "hi";
nested.child = nested;

console.log(nested, nested.child, nested.child.child, "... and so on");

If you run this in the browser console, you can actually keep expanding the child property until... infinity?

Is there a limit to how deep objects an be nested, and what is it?



Solution 1:[1]

There's no limit in the Javascript specification.

If you wanted to find a particular limit in a particular JS engine, you'd have to code a test and see if you could find a limit and running into a limit would probably not be related at all to nesting, but just to number of objects and memory used by them.

Solution 2:[2]

No, there is no limit and you can freely nest further. But, if you keep consolelogging nested objects in nested objects and so on, the program would slow down due to limited memory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rosmak
Solution 2 TeeJaysDev