Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 28 Jun, 2019

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 script to demonstrate 
    // why “0” is not equal to false in
    // ‘if’ condition
    function GFG() {
        // Print type of "0"
        document.write(typeof "0" + "</br>");
        // Print boolean value of "0"
        document.write(Boolean("0") + "</br>");
        // Boolean value of "0" is true so
        // 'if' part will execute
        if("0") {
            document.write("if part executed");
        else {
            document.write("else part executed");
    // Driver code


if part executed
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!