Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Last active October 24, 2018 18:04
Show Gist options
  • Save Alex1990/4e018eea77ec9725403b to your computer and use it in GitHub Desktop.
Save Alex1990/4e018eea77ec9725403b to your computer and use it in GitHub Desktop.
Check if a value is truthy. The word "truthy" means the value is not one of undefined, null, false, 0, NaN or an empty string "".
// Check if a value is truthy. The word truthy means the value is not one of undefined, null, false, 0, NaN or an empty string "".
// Also, you can use the Boolean function to convert the value.
function isTruthy(o) {
return !!o;
}
@akash-mitra
Copy link

This won't work if "0" is passed as the value. Even if the value is zero, isTruthy will return true

@matthias-t
Copy link

@akash-mitra This returns true for "0" and false for 0 because "0" is truthy and 0 is falsy, see MDN

@platinumindustries
Copy link

"0" should be true. Cast to a number if you want to test for 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment