This course has been updated! We now recommend you take the JavaScript: The Hard Parts, v2 course.

Check out a free preview of the full JavaScript: The Hard Parts course:
The "First-Class Objects" Lesson is part of the full, JavaScript: The Hard Parts course featured in this preview video. Here's what you'd learn in this lesson:

Will explains the concept of functions in JavaScript as being first-class objects that can co-exist and be treated like a JavaScript object.

Get Unlimited Access Now

Transcript from the "First-Class Objects" Lesson

>> Will: So how is this all possible? Who comes from a language where the notion at least regularly, there other alternative land or function as attribute. But the basic notion of passing a function in or around into another function is anathema to their day to day behavior. I [INAUDIBLE] use the word anathema.

[00:00:20] Who comes from a language where that's just a bizarre thing to do? Raise your hand if you come from a language that is unusual? Right? What is the language?
>> Speaker 2: HP.
>> Will: Yeah, exactly. That's not the behavior you do by standard. There are ways of doing it, but it is not standard.

[00:00:32] How is it possible? Well, it's because in JavaScript functions are what's called first-class objects. Now that's fairly obscure-sounding. What it just means is they are first-class citizens. That means they behave just like objects. They are actually, literally, objects. How we know we can pass an object into a function, right?

[00:00:54] No problem passing an object into a function. I could pass an object in, I mean this is an object. An array is also an object in JavaScript. I can pass objects in because objects can be values on the right hand side of a label. In JavaScript, just the same, just the same, our functions are objects, which means they can treated like any JavaScript object.

[00:01:17] They can be assigned to variables, known as function expressions. They can be properties of other objects. What was it called when a function's a property of an object? What's a special name?
>> Speaker 2: Method?
>> Will: Method, exactly. They can be passed as arguments into functions. There it is, the function.

[00:01:37] We left a placeholder for it. Instructions, and we passed in an actual function definition as a whole. What a bizarre behavior. JavaScript, no problem cuz its functions are just like objects. And they can even be returned as values from functions. They could be our output, not an array but a whole function definition.

[00:01:59] Which we can then call, invoke, execute out here under a new label. That gives for me the most beautiful concept in JavaScript. Which so often we're doing hard parts, is like a, well I'm afraid we can't look at that, but today we are gonna be doing altogether. When we return a function from a function we see come to light a special concept in JavaScript.

[00:02:23] What is that concept, Ameen? When we return a function from a function?
>> Speaker 2: High order function.
>> Will: It is a high order function by definition but what is the special thing we return a function from a function, Lindsey? Andrea? It is a magical world of, closure, and we'll see that in a second.

[00:02:39] JavaScript functions have one bonus property besides just being objects. What's the bonus property they have? Clara?
>> Speaker 2: Dunno.
>> Will: Katie? They can be evoked. They can have parens on the end of them. You can't do that for objects. That's the only bonus thing for a function, you can run it.

[00:02:58] It has functionality that can be run. Now that sounds, the only, it's also the most profound, it's everything. But it is also the only difference they have from objects.