Functions in Javascript (are objects too!)

I meant to post this great piece from John Resig last year when I came across it, but forgot. Better late than never! If you’re still in the beginning stages with Javascript, like me, you’ve read countless times that “everything is an object.” That’s pretty cool, I guess. But what does that actually mean? Is everything, everything an object? What encompasses everything anyway? Are variables objects? Strings? Numbers? Arrays?

Though it all sounds pretty cool, the problem with the “everything is an object” for beginners is that it’s still fairly abstract. You still don’t know what you can even do with objects—you just know that everything is one.  Seeing that functions were objects was a ground-breaking moment for me. It came when I read these few lines:

// Don’t do this: function getData() { }

// Do this instead: var getData = function() { };

There it is. Plain as day. That function you’re creating can be used just like any old variable. Though there’s no real difference between the two, it’s a fantastic way for beginners (like me!) to think about functions.

Functions are one of the biggest stumbling block for beginners, imo. Getting to the point where you can pass the return value(s) from one function to another is really a holy grail and indicates that you’re actually “getting it.” Hopefully this will help with that.