Javascript: Truthy And Falsy Values

Javascript: Truthy And Falsy Values

Hey there! Welcome to the new year! I hope you smash your goals and every other thing you have set out to achieve.

In this article, we will read about Truthiness and Falsiness in JavaScript. All values have an inherent truthiness and falsiness to them. This concept comes into play mostly in logical operators and conditionals.

A falsy value is a value that is considered false when encountered in a Boolean context. Below are examples of falsy values:

  1. false

  2. Nan (Not a Number)

  3. undefined

  4. null

  5. 0

  6. " ", ' '(an empty string)

In the same way, a truthy value is a value that is considered true when encountered in a Boolean context. Here are some examples:

  1. true

  2. "hello" (the string hello)

  3. 1

  4. [ ] (an empty array)

  5. { } (an empty object)

Using the if/else statements, you can check if a value is either truthy or falsy.

if(0) { 
    console.log("Truthy"); 
} else {
    console.log("Falsy");
} //output will be Falsy