Friday, September 5, 2025
HomeLanguagesJavascriptWhy “0” is not equal to false in if condition in JavaScript...

Why “0” is not equal to false in if condition in JavaScript ?

The reason behind this behavior is that JavaScript treats non-empty string as true. First, “0” is converted into its boolean value, by automatic type conversion which is true. Therefore, if statement executes. 

Example: This example illustrates why “0” is not equal to false in if() condition

javascript




// JavaScript script to demonstrate 
// why “0” is not equal to false in 
// ‘if’ condition 
  
function GFG() { 
  
    // Print type of "0" 
    condole.log(typeof "0" ); 
  
    // Print boolean value of "0" 
    condole.log(Boolean("0") ); 
  
    // Boolean value of "0" is true so 
    // 'if' part will execute 
    if("0") { 
        condole.log("if part executed"); 
    
    else
        condole.log("else part executed"); 
    
  
// Driver code 
GFG();


Output:

string
true
if part executed
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS