What's the point of the Boolean object?

前端 未结 9 1090
遥遥无期
遥遥无期 2020-12-09 15:28

I am reading through the Mozilla Manual on JavaScript, and I come to this point in my reading, Boolean object. I can\'t see a single use for them. What\'s their point? Why w

9条回答
  •  渐次进展
    2020-12-09 15:46

    From the documentation:

    Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. Any object whose value is not undefined , null, 0, NaN, or the empty string , including a Boolean object whose value is false, evaluates to true when passed to a conditional statement.

    Imagine the following scenario:

    if(SomeBoolean){...}
    

    will be true in scenarios where SomeBoolean is a Boolean object.

    Conversely:

    if(false){...}
    

    will always be false

    Addendum for clarification.

    var someString = new Boolean("MyNonEmptyString")
    if(someString) //true
    var otherString = new Boolean("")
    if(otherString) //false
    

提交回复
热议问题