JavaScript - IF Condition - Part 3

Photo by LUM3N on Unsplash

JavaScript - IF Condition - Part 3

Truthy and Falsey states of primitive types

Video Tutorial

Primitive Behaviour

When primitives are used as conditional expressions in the "if statement" they depict a specific behavior. The below table identifies and describes this behavior.

PrimitiveBehaviorDescription
number (eg: 10, -10)TruthyCan be a negative or positive value
number (eg: 0)FalseyZero is falsey
number(eg: 10.14, -10.14)TruthyThe fractional number can be a negative or positive value
string(eg: "Ram")TruthyAny string
empty string( e.g: "")FalseyAn empty string is falsey
nullFalseyIt is falsey
undefinedFalseyIt is falsey
bigint(eg: 999n)TruthyCan be a negative or positive value
bool (eg: true)TruthyBoolean true is truthy
bool(eg:false)FalseyBoolean false if falsey
SymbolTruthyIt is truthy

Use Cases

Case 1: Primitive "undefined" is Falsey.

if(undefined)
    console.log("undefined - true");
else
    console.log("undefined - false");

Case 2: Primitive "null" is Falsey.

if(null)
    console.log("null - true");
else
    console.log("null - false");

Case 3: Primitive "number" is Truthy (When positive or negative)

if (100) // try -100
    console.log("100 - true");

Case 4: Primitive "number" is Falsey (When the value is 0)

if(0)
    console.log("0 - true");
else
  console.log("0 - false");

Case 5: Primitive "string" is Truthy (When it contains a value)

if("Ram")
    console.log("Ram - true");

Case 6: Primitive "string" is Falsey (When it contains nothing)

if("")
    console.log("Empty string - true");
else
    console.log("Empty string - false");

Case 7: Primitive null+1 is Truthy

if(null+1)
    console.log("null+1 - true");

Case 8: Primitive undefined+1 is Falsey

if(undefined+1)
    console.log("undefined+1 - true");
else
    console.log("undefined+1 - false");

Case 9: Primitive Symbol is Truthy

const sym = Symbol();
const sym2 = Symbol("ID");

if(sym) // try - sym2
    console.log("Empty string - true");

Case 10: Value NaN is Falsey

if(NaN)
    console.log("NaN - true");
else
    console.log("NaN - false");

Complete Code Listing


/*
Author: Mahavir DS Rathore
Objective: Truthy and Falsey scenarios with primitives
*/


if (undefined)
  console.log("undefined - true");
else
  console.log("undefined - false"); // false

if(null)  
console.log("null - true");
else
  console.log("null - false"); // false


if(NaN)  
console.log("NaN - true");
else
  console.log("NaN - false"); // false


if(-1)  
    console.log("-1 : true"); // true
else
    console.log("-1 : false"); 

if(2)  
        console.log("2 : true");//true
else
      console.log("2 : false"); 

if("ram")  
    console.log("ram - true"); // true
else
  console.log("ram - false"); 

if("")  
  console.log("empty - true"); 
else
  console.log("empty - false");  // false



if(null+1)  
    console.log("null+1 : true",null+1); // true 1
else
  console.log("null+1 : false"); 


if(undefined+1)  
    console.log("undefined+1 : true"); 
else
  console.log("undefined+1 : false", undefined+1); //false  NaN


const sym = Symbol();
const sym2 = Symbol("ID");

if (sym) // truthy
    console.log("sym - true");

if(sym2)   // truthy 
  console.log("sym2 - true");

Video Tutorial

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!