xxxxxxxxxx
// Truthy values
/*
true
{}
[]
42
"0"
"false"
new Date()
-42
12n
3.14
-3.14
Infinity
-Infinity
*/
// Falsy values
/*
false
0
-0
0n
"", '', ``
null
undefined
NaN
*/
xxxxxxxxxx
// this alls are truthy values including empty object and array,
// excluding empty string (empty string is falsy value)
if ([]) {
console.log('✅ This runs');
}
if ({}) {
console.log('✅ This runs');
}
if (true) {
console.log('✅ This runs');
}
if ('test') {
console.log('✅ This runs');
}
xxxxxxxxxx
let value = ""; // Assigning an empty string
if (value) {
console.log("Truthy"); // This block will not execute
} else {
console.log("Falsy"); // This block will execute
}