What is the full form of an expressionless statement in javascript?

This question already has an answer here:

  • Detecting an undefined object property 40 answers

  • if (foo) { 
    }
    

    "What is this equivalent to?"

    It's not equivalent to any of the ones you suggested. It would be equivalent to:

    if (Boolean(foo)) { }
    

    or the same thing by using the ! operator:

    if (!!foo) { }
    

    or you could be explicit in your comparison if you really want.

    if (!!foo === true) { }
    

    "My actual motivation for asking is wanting to ensure that a member "exists"..."

    To find if a member exists in an object, use the in operator.

    if ("devicePixelRatio" in window) { }
    

    "... (that is to say, if it is null or undefined then it doesn't exist):"

    To check for not null or undefined , which is no the same as not existing, do this:

    if (window.devicePixelRatio != null) { }
    

    The != operator will perform both a null and undefined check at the same time. Any other value will meet the condition.

    链接地址: http://www.djcxy.com/p/22642.html

    上一篇: 我如何检查变量是否存在

    下一篇: javascript中表达式语句的完整形式是什么?