Wednesday, August 18, 2021

Truthy and Falsy Value in Javascript Basic Guideline

5 Falsy Value in Javascript (When convert in Boolean)

  • 0 (Zero)
  • ' ' (Blank)
  • undefined
  • null
  • NaN (Not a Number)

Truthy Value in Javascript : Everything Except Above like Numbers and Strings and Object

// For Example ....

console.log(Boolean(0)); // Print False

console.log(Boolean(undefined)); // Print False

console.log(Boolean('Anil')); // Print True

console.log(Boolean({})); // Print Ture



// Example for 0 in Condition ...

const money = 0;

if (money) { // This Became false becoz of 0 is falsy value in condition

console.log(`Don't Spend All Money`);

} else {

console.log(`You Need a Job to Earn`);

}

// Using undefined falsy value
let height;
if (height) {
console.log(`YAY ! Height is Defined`);
} else {
console.log(` Height is not defined`); // Print this bcoz height is undefined.
}

0 comments

Post a Comment