DEV Community

Discussion on: How to navigate complex objects in 1 line of js

Collapse
 
karataev profile image
Eugene Karataev

Great one-line helper!
I think the post misses the the motivation behind navigator function. Indeed, why use navigator(test, 'a.b.c') instead of short test.a.b.c?
This navigator function helps to avoid runtime errors when trying to read properties from null or undefined.
test.z.x will throw, but navigator(test, 'z.x') will return undefined.
This is such a common problem, so TC39 proposes optional chaining with syntax test?.a?.b?.c which is currently in stage2.