Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Why “0” is equal to false in JavaScript ?

  • Last Updated : 27 Jun, 2019

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.

Example: This example illustrates why “0” equals to false.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

    // JavaScript code to demonstrate 
    // why “0” is equal to false
    function GFG() {
        // Print type of "0"
        document.write(typeof "0" + "</br>");
        // Test whether "0" equals to false
        // or not. If "0" is equal to false
        // then "0" == false i.e.
        // false == false which return true
        var result = ("0" == false);
        // Print result
        document.write(result + "</br>");
        // Convert and print "0" to its numeric
        // value
        document.write(Number("0") + "</br>");
        // Convert and print false in numeric value
        document.write(Number(false) + "</br>");
        // So both numeric value are same
        // Therefore condition "0" == false
        // evaluates to true
        document.write("0" == false);
        // Or this statement
        document.write(Number("0") == Number(false));
    // Driver code



So, from above it is clear that “0” is equal to false and reason behind this behavior is also clear, but when “0” is tested in if condition then it evaluates to true.

My Personal Notes arrow_drop_up
Recommended Articles
Page :