What does "if(!variable)" check?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yansky

    What does "if(!variable)" check?

    If I have the following code:

    var abc;

    if(!abc){

    alert('test');

    }

    Does "if(!abc)" check to see if the variable is null or does it check
    to see if the variable exists at all?
  • Joost Diepenmaat

    #2
    Re: What does "if(!varia ble)" check?

    Yansky <thegooddale@gm ail.comwrites:
    If I have the following code:
    >
    var abc;
    >
    if(!abc){
    >
    alert('test');
    >
    }
    >
    Does "if(!abc)" check to see if the variable is null or does it check
    to see if the variable exists at all?
    In that code, abc always exists.

    Anyway ! EXPRESSION:

    Throws a reference error if the EXPRESSION resolves to a property of
    which the base object can't be determined; loosly: an undeclared
    variable. (As far as I can tell. This part of the specs has some pretty
    deep implied consequences).

    If the expression does resolve to a value (which includes declared but
    not explicitly initialized global variables and never-before used
    properties of explicit objects, which will be undefined) it will first
    convert the EXPRESSION to a boolean and then return the inverse.

    The standard ToBoolean conversion is:

    undefined -false
    null -false
    boolean -boolean
    numbers 0, -0, NaN -false
    other numbers -true
    empty string -false
    other string -true
    object -true

    See ecma 262 3rd edition, section 9.2.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Tom de Neef

      #3
      Re: What does &quot;if(!varia ble)&quot; check?

      "Yansky" <thegooddale@gm ail.com>
      >
      var abc;
      >
      if(!abc){
      >
      alert('test');
      >
      }
      >
      Does "if(!abc)" check to see if the variable is null or does it check
      to see if the variable exists at all?
      ECMA262
      (http://www.ecma-international.org/pu...T/Ecma-262.pdf)
      11.4.9
      the expression abc is evaluated and transformed via toBoolean(abc)
      then the NOT operator: if the result is true then return false else return
      true

      toBoolean(arg) returns false when arg is one of the following:
      undefined
      Null
      boolean - false
      number - +0, -0, NaN
      String - when empty

      Tom


      Comment

      • Yansky

        #4
        Re: What does &quot;if(!varia ble)&quot; check?

        On Apr 13, 7:01 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
        Yansky <thegoodd...@gm ail.comwrites:
        If I have the following code:
        >
        var abc;
        >
        if(!abc){
        >
        alert('test');
        >
        }
        >
        Does "if(!abc)" check to see if the variable is null or does it check
        to see if the variable exists at all?
        >
        In that code, abc always exists.
        >
        Anyway ! EXPRESSION:
        >
        Throws a reference error if the EXPRESSION resolves to a property of
        which the base object can't be determined; loosly: an undeclared
        variable. (As far as I can tell. This part of the specs has some pretty
        deep implied consequences).
        >
        If the expression does resolve to a value (which includes declared but
        not explicitly initialized global variables and never-before used
        properties of explicit objects, which will be undefined) it will first
        convert the EXPRESSION to a boolean and then return the inverse.
        >
        The standard ToBoolean conversion is:
        >
        undefined -false
        null -false
        boolean -boolean
        numbers 0, -0, NaN -false
        other numbers -true
        empty string -false
        other string -true
        object -true
        >
        See ecma 262 3rd edition, section 9.2.
        >
        --
        Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
        Thanks, I get it now.

        Comment

        Working...