Falsey values in JavaScript

后端 未结 6 1320
情歌与酒
情歌与酒 2020-11-27 06:13

I had an interesting interview question today that stumped me a little. I was asked about falsey values. So undefined, NaN, null, 0, and an empty string all evaluate to fals

6条回答
  •  执念已碎
    2020-11-27 06:52

    They're also useful for setting default values...

    function foo(bar){
        alert(bar || "default");
    }
    

    I know a lot of people try to do

    if (typeof(foo) === "undefined"){}
    

    to get around falsiness, but that's got its own problems because

    typeof(null) === "object"
    

    for some reason

提交回复
热议问题