Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Last active January 23, 2021 10:33
Show Gist options
  • Save YujiSODE/f911679874ad204c09323c91c0e09879 to your computer and use it in GitHub Desktop.
Save YujiSODE/f911679874ad204c09323c91c0e09879 to your computer and use it in GitHub Desktop.
memo: Truthy values and falsy values in JavaScript

memo: Truthy values and falsy values in JavaScript

Truthy values

Value Boolean value Example
true true !!(true); //true
1 true !!(1); //true
-1 true !!(-1); //true
3.14 true !!(3.14); //true
-3.14 true !!(-3.14); //true
12n true !!(12n); //true
Infinity true !!(Infinity); //true
-Infinity true !!(-Infinity); //true
"0" true !!("0"); //true
--- --- ---
{} true !!({}); //true
{a:1,b:2,b:3} true !!({a:1,b:2,b:3}); //true
[] true !!([]); //true
[1,2,3] true !!([1,2,3]); //true
"false" true !!("false"); //true
--- --- ---
function(){} true !!(function(){}); //true
()=>{} true !!(()=>{}); //true

Falsy values

Value Boolean value Example
false false !!(false); //false
0 false !!(0); //false
-0 false !!(-0); //false
0n false !!(0n); //false
-0n false !!(-0n); //false
--- --- ---
Empty string ("") false !!(""); //false
null false !!(null); //false
undefined false !!(undefined); //false
NaN false !!(NaN); //false
document.all false !!(document.all); //false
--- --- ---
function(){}() false !!(function(){}()); //false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment