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

3110Re: "Unexpected use of typeof" warning discrepancy

Expand Messages
  • douglascrockford
    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.
    • Show all 3 messages in this topic