Bitesize Bugs

Zero is ‘falsy’ in JavaScript

Numeric zero in JavaScript is a ‘falsy’ value.
If it is used in a conditional operator it will act as false.

Example:

var checkValue = function(value){
    var check = "";

    if(value){
       check = "it was "+value;
    }
    else{
       check = "no value";
    }

    return check;
}

// number; returns "no value"
console.log(checkValue(0)); 

// string; returns "it was 0"
console.log(checkValue("0"));

Related reading:

Falsy values – extract from “JavaScript: The Good Parts”
Truthy and falsy in JavaScript

Advertisements

Filed under: Front-end, JavaScript, , , , , ,

So what’s it about?

Bitesize Bugs is a collection of short articles that expose pesky bugs in front-end web technologies like JavaScript, CSS, HTML and browser behaviors gone bad.