1

When I'm writing code that relies on objects created by another script file referenced from the same page I often find myself having to test for the existence of some global variable or another.

I'd expect that the logical idiom to do that would be as follows:

if (window.myLibrary) myLibrary.someFunction();

However after some research on stackoverflow, the consensus seems to be that the correct idiom is this:

if (typeof myLibrary != "undefined") myLibrary.someFunction();

This idiom is given in several answers on stackoverflow, the most prominent example being this one:

How can i check whether a variable is defined in javascript

However, I can't find any explanation for why the second version is preferable. All the recommendations for it are in the form "just use this." Can someone please explain why using the typeof operator is superior to checking for the object by referencing it as a property of the window object?

Community
  • 1
  • 1
Racheet
  • 730
  • 8
  • 21
  • Your first example tests for truthyness, while the second tests for definition. For this scenario, either one is probably fine, but might make more sense to use `if (typeof myLibrary === "function")` – Ian May 17 '13 at 14:53
  • In the use case I'm talking about, I only need to verify truthyness. I want to know if another library is loaded on that page and I thought that I could assume that if the variable doesn't resolve to false then the library is loaded. One of the reasons I asked this question was to see if that was an acceptable assumption. – Racheet May 17 '13 at 14:54
  • I think it's fine to use your first example then. It's more important for literal values, because they can have falsey values, where `if (val)` would show inconsistent results when checking for definition. By the way, I had a typo in my first comment - if you wanted to very specific, you could use `if (myLibrary && typeof myLibrary.someFunction === "function")` – Ian May 17 '13 at 15:00
  • If you only need to know if its true or not I think your first solution would be just fine. – Geo May 17 '13 at 15:01
  • Again, specifically for your scenario, the first example should be fine. The answer to the StackOverflow question you included is basically a universal way of detecting if a variable is defined or not. If you wanted to see if an actual **variable** was **defined**, your first example would throw an error. For example, `if (someLocalVar)` would throw an error if there literally wasn't a variable declared at all on the page with the name "someLocalVar". Testing on `window` and the global scope is different – Ian May 17 '13 at 15:08

2 Answers2

4

myLibrary could exist but be set to 0 or false in which case the first example will assume it doesn't exist at all.

Rob Johnstone
  • 1,704
  • 9
  • 14
3

I tend to go for the first example, unless there is some really compelling reason why someone might set the library to "true" or something similar. Yes, if your code is running in someone else's environment (ie, you're building a public Javascript framework like JQuery) you may want to be as safe as possible with the type-checking, to ensure someone isn't misusing the library. But in my view, perfect type safety in Javascript is not quite so important as to make all your code extremely verbose, and occasionally difficult to read.

I also liked an answer from the linked question; you can use

if ('myLibrary' in window)

I think that one's just preference. To clarify, that one will evaluate true even if window.myLibrary is 'false', so you'd basically be checking whether it was defined at all.

Katana314
  • 8,429
  • 2
  • 28
  • 36
  • Personally, I find the second if statement clearer but maybe that explains why I'm spending a Friday night browsing stackoverflow ;) – Rob Johnstone May 18 '13 at 00:20