Douglas Crockford: Tonight is Act III: Function the Ultimate. We're going to be talking about functions tonight. Functions are the very best part of JavaScript. It's where most of the power is, it's where the beauty is. Like everything else in JavaScript, they're not quite right, but you can work around that, and there's a lot of good stuff here.

Tonight, unlike the previous two nights, I'm going to be showing you quite a lot of code. Because we're talking about functions, you need to see how they work. I personally tend to fall asleep in presentations that put a lot of code on the screen; it's just kind of not a good time, so I have a lot of examples and I tried to make them all fit on one screen in big type. They're all going to be simple, but they should be interesting and useful. Let's begin.

Function is the key idea in JavaScript. It's what makes it so good and so powerful. In other languages you've got lots of things: you've got methods, classes, constructors, modules, and more. In JavaScript there's just function, and function does all of those things and more. That's not a deficiency, that's actually a wonderful thing — having one thing that can do a lot, and can do it brilliantly, at scale, that's what functions do in this language.

Here's what a function is. A function is the word 'function'. It optionally has a name, which can be used to allow it to call itself. It can have a set of parameters, which are wrapped in parens, containing zero or more names which are separated by commas. It can have a body which is wrapped in curly braces, containing zero or more statements. A function expression like that produces an instance of a function object. Function objects in this language are first class, which means that they can be passed as an argument to another function, they may be returned as a return value from a function, they can be assigned to a variable, and they can be stored in an object or an array.

Anything you can do with any other kind of value in this language, you can do with a function. A function expression is like an object literal in that it produces a value, except in this case it produces something that inherits from Function.prototype. It may seem kind of strange that a function can inherit methods from something else, but it can. So in this language, functions have methods. That may sound odd, but we've got that. I'll show you some examples of that.

We have a var statement which allows us to declare and initialize variables within a function. Because JavaScript is not a strongly typed language you don't specify types in the var statement, you just give a name for the variable. Any variable can contain any value that's expressible in the language. A variable that's declared anywhere within a function is visible everywhere within the function; we don't respect block scope.

The way var statements work is the var statement gets split into two pieces. The declaration part gets hoisted to the top of the function and is initialized with undefined. Back at the place where the original var statement was, it gets turned into an assignment statement so that the var gets removed. Here we have an example. I've got myVar = 0 and myOtherVar. What that does is, at the top of the function it defines myVar and myOtherVar and sets them both to undefined. Then at the point in the function where the original var statement was, we have an assignment statement.