Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Object JavaScript - avoid property access errors
// Here’s a verbose technique:
var motherName = undefined;
if(spot) {
if (spot.mother) motherName = spot.mother.name;
}
// And here is a more concise way.
var motherName = spot && spot.mother && spot.mother.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment