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

2925Re: typeof null

Expand Messages
  • spence.randall@ymail.com
    Jul 19, 2012
      I foresee a minor problem with this: in most browsers, undefined is not read-only and can be overwritten, which means if you have other code on a page outside of your control, it's safer to do a typeof === 'undefined' check rather than x === undefined.

      For example:

      var undefined = 1,
      x;
      alert(x === undefined);

      Will alert false, whereas

      var undefined = 1,
      x;
      alert(typeof x === undefined);

      will alert true, as expected.

      Note, only Firefox seems to throw an error saying undefined is read-only.

      Granted it is irresponsible and insanely stupid to redefine undefined, but their are some irresponsible and insanely stupid people out in the wild writing code, the later test at least seems more bullet proof.

      Thanks,
      Randall

      --- In jslint_com@yahoogroups.com, "douglascrockford" <douglas@...> wrote:
      >
      > JSLint will now warn if you write
      >
      > typeof x === 'null' || typeof x === 'undefined'
      >
      > instead of
      >
      > x === null || x === undefined
      >
    • Show all 10 messages in this topic