Loading ...
Sorry, an error occurred while loading the content.

"Unexpected use of typeof" warning discrepancy

Expand Messages
  • andrew_wh
    I understand why the following code generates the warning Unexpected typeof . Use === to compare directly with undefined var foo; if (typeof foo ===
    Message 1 of 3 , Jan 29, 2013
    • 0 Attachment
      I understand why the following code generates the warning "Unexpected 'typeof'. Use '===' to compare directly with undefined"

      var foo;

      if (typeof foo === 'undefined') {
      alert('foo undefined');
      }

      JSLint wants me to write this instead:

      var foo;

      if (foo === undefined) {
      alert('foo undefined');
      }

      Which is all well and good.

      However I noticed that I can get around the error by simply switching the operands in the typeof expression:

      var foo;

      if ('undefined' === typeof foo) {
      alert('foo undefined');
      }

      Is this by design? Am I missing something silly and fundamental here? Or is it a bug in JSLint?
    • douglascrockford
      ... Thanks. Please try it now.
      Message 2 of 3 , Jan 29, 2013
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "andrew_wh" wrote:
        >
        > I understand why the following code generates the warning "Unexpected 'typeof'. Use '===' to compare directly with undefined"
        >
        > var foo;
        >
        > if (typeof foo === 'undefined') {
        > alert('foo undefined');
        > }
        >
        > JSLint wants me to write this instead:
        >
        > var foo;
        >
        > if (foo === undefined) {
        > alert('foo undefined');
        > }
        >
        > Which is all well and good.
        >
        > However I noticed that I can get around the error by simply switching the operands in the typeof expression:
        >
        > var foo;
        >
        > if ('undefined' === typeof foo) {
        > alert('foo undefined');
        > }
        >
        > Is this by design? Am I missing something silly and fundamental here? Or is it a bug in JSLint?


        Thanks. Please try it now.
      • andrew_wh
        Looks like it s been fixed. Thanks for the quick turnaround!
        Message 3 of 3 , Jan 29, 2013
        • 0 Attachment
          Looks like it's been fixed. Thanks for the quick turnaround!

          --- In jslint_com@yahoogroups.com, "douglascrockford" wrote:
          >
          > --- In jslint_com@yahoogroups.com, "andrew_wh" wrote:
          > >
          > > I understand why the following code generates the warning "Unexpected 'typeof'. Use '===' to compare directly with undefined"
          > >
          > > var foo;
          > >
          > > if (typeof foo === 'undefined') {
          > > alert('foo undefined');
          > > }
          > >
          > > JSLint wants me to write this instead:
          > >
          > > var foo;
          > >
          > > if (foo === undefined) {
          > > alert('foo undefined');
          > > }
          > >
          > > Which is all well and good.
          > >
          > > However I noticed that I can get around the error by simply switching the operands in the typeof expression:
          > >
          > > var foo;
          > >
          > > if ('undefined' === typeof foo) {
          > > alert('foo undefined');
          > > }
          > >
          > > Is this by design? Am I missing something silly and fundamental here? Or is it a bug in JSLint?
          >
          >
          > Thanks. Please try it now.
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.