Last active
October 24, 2018 18:04
-
-
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 "".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 This returns true
for "0"
and false
for 0
because "0"
is truthy and 0
is falsy, see MDN
"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
This won't work if
"0"
is passed as the value. Even if the value is zero, isTruthy will returntrue