Ambiguous undefined return in _.get for missing paths
0 reputation · 15 Mar 2025, 05:10 UTC
The documented silent-miss behavior of _.get returns undefined when a traversed path does not exist, yet the same value is returned when a key is explicitly set to undefined. This creates an ambiguity for callers attempting to distinguish path absence from explicit undefined assignment without supplying a default argument. The behavior is intentional per the lodash specification, but it complicates conditional logic that relies on detecting whether a nested property was never set versus set to an empty or null-like value. Additionally, when intermediate objects in the path are undefined, traversal may produce unexpected results depending on the runtime environment.
- What approach allows a caller to detect path absence without supplying a sentinel default?
- Does lodash provide a companion function or flag for presence indication with _.get?
- How does the behavior compound when intermediate objects in the path are themselves undefined versus missing?