Skip to content

Instantly share code, notes, and snippets.

@rileybracken
Last active January 19, 2018 23:15
Show Gist options
  • Save rileybracken/24c91dc74122e50afb79ba22fe32f249 to your computer and use it in GitHub Desktop.
Save rileybracken/24c91dc74122e50afb79ba22fe32f249 to your computer and use it in GitHub Desktop.
const delivery = {
item: null,
};
const andAndMethod = (delivery && delivery.item && delivery.item.name) || "Untitled Product";
const typeOfMethod =(
typeof delivery != 'undefined' &&
typeof delivery.item != 'undefined' &&
typeof delivery.item.name != 'undefined' &&
delivery.item.name
) || "Untitled Product");
console.log(andAndMethod); // "Untitled Product"
console.log(typeOfMethod);
// "Error" (this is because item is null and it is checking for typeof undefined,
// more than likely it just wouldn't be there, but it IS possible it would be null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment