Skip to content

Passing undefined to JavaScript Immediately-invoked Function Expressions

Sometimes you can spot old code in the wild where `undefined` is passed to a function. Why?

I discovered this little trick while watching the famous Paul Irish video about the jQuery source code.

That video comes from a different era and it’s 9 years old at the time of writing, and the jQuery source code has changed since, so you can’t spot this thing in there, but it’s still something I found interesting.

Also, JavaScript has since changed. This technique only applied to pre-ES5 JavaScript.

Before ES5, released in 2009, this was an almost required step.

Note: ES5+ codebases do not need to add this any more because now undefined is a read-only value.

Sometimes in our code we do check variables to see if they are undefined, in this way:

if (car !== undefined) {

}

If this is our code, that runs on our own servers, which we control, this should work fine. But imagine that a library like jQuery needs to be battle tested, to work on every possible site.

If someone overwrites undefined with a simple

undefined = '🤔' //whatever value you prefer

then the above if would fail, comparing car to 🤔.

This has since been fixed in ES5, but was possible before that.

If car was actually undefined, there was no way to find out now.

Except using this technique: we wrap all our code in an IIFE (Immediately-invoked Function Expression) and we pass one parameter to the function definition, without adding it in the invocation phase.

(function() {
  /* our function code */
})()
(function(undefined) {
  /* our function code */
})()

See, undefined is passed as argument, but not passed as a parameter when we invoke the function. So, inside the function the value of the variable undefined is (guaranteed) the original value of undefined. No matter what other scripts on the page do to it, it’s isolated.

Now, my favorite way to solve this problem is to use this technique to check for undefined values:

if (typeof car !== 'undefined') {

}

The typeof operator returns a string with the type of a variable. We can check it against the 'undefined' string, and we wouldn’t have the above problem in the first place.

But it’s always good to know the reasons about some things you can read on code written by others, especially when it’s library-level code that need to run everywhere.


→ Get my JavaScript Beginner's Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about js: