Functions are Objects

As mentioned, everything that is not a primitive is an object, that includes functions too… I know weird right? Kinda hard to think of functions as a group of key/value pairs. Since functions are objects they are often referred to as function-objects. A function-object is a special group of key/value pairs with specific properties for executing code and passing down values. We’ll go over what these properties are in the next section. First let’s talk about why functions are important.

You could say that function-objects have two primary purposes. If we want to create a chuck of logic that executes, we can use a function-object: just like “methods” in any other programming language. The next purpose is where JavaScript gets kinda funky. If we want to create objects with both values and methods, and perhaps some logic to set those values, we would also use function-objects. This is where you could think of function-objects as behaving like “classes” in object-oriented languages (i.e. Java/C#).

Note: You will hear the term “method” sometimes used in JavaScript. In JavaScript a method refers to a function-object which exists as a property of a containing object.

In the standard case, functions in JavaScript look like functions in any other language; they execute a chunk logic to perform a specific task. In the following snippet, bark is function-object executing code.

function bark() {

console.log('woof woof')

} bark() => 'woof woof'

If we wanted to package a small group of data, like our 2 properties in the Dog object from before, then a simple key/value pair list works just fine. What if we want to create multiple Dog objects though? Maybe some values need to be static and others dynamic. This is where function-objects come in handy. When we call a function with new , an object (aka instance-object) is returned with properties set from the this keyword inside the function.