Skip to content

Instantly share code, notes, and snippets.

@waiteb3
Created October 5, 2016 21:48
Show Gist options
  • Save waiteb3/a65ae299e16e68b65ce4308a3447cfae to your computer and use it in GitHub Desktop.
Save waiteb3/a65ae299e16e68b65ce4308a3447cfae to your computer and use it in GitHub Desktop.
// JavaScript has 6 falsy values (that evaluate to false when coerced to a boolean)
// false
// 0
// ""
// null
// undefined
// NaN
// Using != null, we can safely check against null and undefined rather than multiple underscore functions, typeofs, etc
var obj = {
zero: 0,
falsed: false,
empty: "",
nan: NaN,
nulled: null,
undef: undefined,
};
// the ones we don't want to evalute as false
// 1 :- [object Object]
// 2 :- 0
// 3 :- false
// 4 :- ""
// 5 :- NaN
console.info("1 :- " + (obj != null ? obj : null));
console.info("2 :- " + (obj.zero != null ? obj.zero : null));
console.info("3 :- " + (obj.falsed != null ? obj.falsed : null));
console.info("4 :- " + (obj.empty != null ? '""' : null));
console.info("5 :- " + (obj.nan != null ? obj.nan : null));
// The ones we care about
// 6 :- null
// 7 :- null
console.info("6 :- " + (obj.undef != null ? obj.undef : null));
console.info("7 :- " + (obj.nulled != null ? obj.undef : null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment