Last active
February 22, 2020 11:25
-
-
Save romankierzkowski/9024908 to your computer and use it in GitHub Desktop.
True, False and Equal in JS
This file contains hidden or 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
/* false, 0, undefined, null, NaN, "" are false */ | |
> Boolean(false) | |
false | |
> Boolean(0) | |
false | |
> Boolean("") | |
false | |
> Boolean(undefined) | |
false | |
> Boolean(null) | |
false | |
> Boolean(NaN) | |
false |
This file contains hidden or 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
/* Anything else is true: | |
*/ | |
> Boolean(true) | |
true | |
> Boolean(1) | |
true | |
> Boolean(2) | |
true | |
> Boolean("Hello world!") | |
true | |
/* Including empty array, Python developers!!! */ | |
> Boolean([]) | |
true |
This file contains hidden or 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
/* Let's try equal: */ | |
> false == false | |
true | |
> 0 == false | |
true | |
> "" == false | |
true | |
/* So far, so good, but... : */ | |
> undefined == false | |
false | |
> null == false | |
false | |
> NaN == false | |
false | |
This file contains hidden or 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
/* The same for truthy works well: */ | |
> true == true | |
true | |
> 1 == true | |
true | |
/* But: */ | |
> 2 == true | |
false | |
> "Hello world!" == true | |
false | |
> [] == true | |
false |
This file contains hidden or 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
/* Let's avoid type coerition with triple-equals: */ | |
> false === false | |
true | |
> 0 === false | |
false | |
> "" === false | |
false | |
> undefined === false | |
false | |
> null === false | |
false | |
> NaN === false | |
false | |
/* Good. And for truthy: */ | |
> true === true | |
true | |
> 1 === true | |
false | |
> 2 === true | |
false | |
> [] === true | |
false | |
> "Hello world!" === true | |
false | |
/* Tripple equals returns always false when types of operands does not match." */ |
This file contains hidden or 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
/* Equal is 'generous' about the type: */ | |
> true == '1' | |
true | |
> true == '1.0' | |
true | |
/* Triple equals is not: */ | |
> true === '1' | |
false | |
> true === '1.0' | |
false | |
/* It only goes with numbers: */ | |
> 2 == '2' | |
true | |
/* But: */ | |
> true == 'true' | |
false | |
This file contains hidden or 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
> Boolean(' ') | |
true | |
> Boolean(' \n\n') | |
true | |
/* But: */ | |
> ' ' == 0 | |
true | |
> ' \n' == 0 | |
true | |
> ' \n' == false | |
true | |
/* o_O ?? WAT */ | |
/* Thank god for === */ | |
> ' ' === 0 | |
false | |
> ' \n' === 0 | |
false | |
> ' \n' === false | |
false |
This file contains hidden or 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
/* These are quite obvious: */ | |
> undefined == undefined | |
true | |
> undefined === undefined | |
true | |
> null == null | |
true | |
> null === null | |
true | |
/* These are not: */ | |
> null == undefined | |
true | |
> null === undefined | |
false | |
/* But to check if global variable is not defined you can't do the following: */ | |
> foo === undefined | |
ReferenceError: foo is not defined | |
/* There is an exception because foo is not defined. That's why: */ | |
> typeof foo === 'undefined' | |
true | |
/* Typeof returns string containing name of a type. And undefined has type undefined, whereas: */ | |
> typeof null | |
"object" | |
> typeof undefined | |
"undefined" |
This file contains hidden or 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
/* And these are useful for making default result: */ | |
> null || "abc" | |
"abc" | |
> undefined || "abc" | |
"abc" | |
> NaN || "abc" | |
"abc" |
This file contains hidden or 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
/* Watch out for NaN! */ | |
> NaN + NaN | |
NaN | |
// NaN are not equal: | |
> NaN == NaN | |
false | |
> NaN === NaN | |
false | |
// Think about it! A dog and a cat are Not a Number, but they are not equall as well. It make sense. | |
// Use Number.isNaN() instead: | |
> Number.isNaN(NaN) | |
true | |
> Number.isNaN(0) | |
false | |
> NaN && NaN | |
NaN | |
> NaN || NaN | |
NaN | |
// But: | |
> NaN || true | |
true | |
> true || NaN | |
true | |
> false && NaN | |
false | |
> NaN && false | |
NaN | |
// It all make sense, because condition is not evaluated after it's result is certain. | |
// But now: | |
> NaN | NaN | |
0 | |
> NaN & NaN | |
0 | |
// o_O ?? // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice summary! At some points JavaScript is just stupid but
===
makes things a lot better.