Photo by Thought Catalog on Unsplash
JavaScript - IF condition - Part 2
Understanding the Truthy and Falsey expressions
Video Tutorial
What are Truthy and Falsey states?
The If condition executes when an expression returns Truthy value. If condition will execute else statement when the expression returns Falsey value.
A Truthy value is a value that is considered "true" when used in a boolean context.
A Falsey value is a value that is considered "false" when used in a boolean context.
Use cases: Truthy and Falsey values
Case 1: If the condition is a boolean true - it is considered to be Truthy.
if (true)
console.log("Truthy value - true");
Case 2: If the conditional expression is {} (empty object). It is considered a Truthy value.
if({})
console.log("Truthy value - {}");
Case 3: If the conditional expression is [] (empty array). It is considered a Truthy value.
if([])
console.log("Truthy value- []");
Case 4: If the conditional expression is a positive number. It is considered a Truthy value.
if(55)
console.log("Truthy value - 55");
Case 5: If the conditional expression is 0 within quotes(e.g "0"). It is considered a Truthy value. It is nothing but a string.
if("0")
console.log("Truthy value - '0'");
Case 6: If the conditional expression is 0. It is considered a Falsey value.
if(0)
console.log("Truthy value - 0");
else
console.log("Falsey value - 0");
Case 7: If the conditional expression is false. It is considered a Falsey value.
if(false)
console.log("Truthy value - false");
else
console.log("Falsey value - false");
Case 8: If the conditional expression is "false" (within quotes). It is considered a Truthy value.
if("false")
console.log("Truthy value - 'false'");
Case 9: If the conditional expression is an object e.g new Date(). It is considered a Truthy value.
if (new Date())
console.log("Truthy value - new Date()");
Case 10: If the conditional expression is a negative number. It is considered a Truthy value.
if(-200)
console.log("Truthy value : -200");
Case 11: If the conditional expression is a bigint. It is considered a Truthy value.
if(999n)
console.log("Truthy value - 999n")
Case 12: If the conditional expression is a fractional value. It is considered a Truthy value.
if(343.114)
console.log("Truthy value - 343.114");
Case 13: If the conditional expression is a negative fractional value. It is considered a Truthy value.
if(-2343.14)
console.log("Truthy value : -2343.14);
Case 14: If the conditional expression is Infinity. It is considered a Truthy value.
if(Infinity)
console.log("Truthy value - Infinity");
Case 15: If the conditional expression is negative Infinity. It is considered a Truthy value.
if (-Infinity)
console.log("Truthy value : -Infinity);
Complete Code Listing
/*
Author : Mahavir DS Rathore
Objective : Understanding 'if condition' behaviour with -- truthy & falsey values
*/
// a truthy value is a value that is considered true when used in a Boolean context
// a falsy value is a value that is considered false when used in a Boolean context
if (true) // truthy value
console.log("true");
if ({}) // truthy value
console.log("{}");
if ([]) // truthy value
console.log("[]");
if (55) // truthy value
console.log("42");
if ("0") // truthy value
console.log("0");
if(0) // falsey value
console.log("true");
else
console.log("false"); // false
if ("false") // truthy value
console.log("false");
if(false) // falsey value
console.log("true");
else
console.log("false"); // false
if (new Date()) // truthy value
console.log("new Date()");
if (-89) // truthy value
console.log("-42");
if (90n) // truthy value
console.log("12n");
if (343.114) // truthy value
console.log("3.14");
if (-2343.14) // // truthy value
console.log("-3.14");
if (Infinity) // truthy value
console.log("Infinity");
if (-Infinity) // truthy value
console.log("-Infinity");