Skip to content
Lydia Hallie theavocoder
Course contents
Complete JavaScript / Lesson 08

Functions

In order to run certain blocks of code, we can use functions. Whenever a function is invoked, the code inside the function gets executed. We can then return something from that function, although that’s not always necessary.

One way to define a function is to type the keyword “function”, followed by the name of that function. In between the curly brackets, we can specify what we want that function to do or what value to return.

You invoke a function by typing the name of that function, followed by parentheses.

Functions diagram 1

We can pass arguments (or parameters) to functions by adding values between the brackets. In order for a function to be able to receive arguments, we need to specify it when defining our function.

Functions diagram

Function expressions

There are two types of function expressions: named, and anonymous. The difference get clear when we declare a variable to a function:

Functions diagram

Function statements

A function statement declares a function, and makes it possible for us to invoke the function any time.

Functions diagram

The difference between function statements and function expressions will make a lot more sense when we get to hoisting!

Arrow functions

Since the new version of JavaScript, ES2015 got introduced, we have a shorter way of writing functions!

Functions diagram 5

You can write arrow functions inline, which makes it possible for you to save a lot of space.

Functions diagram

Once we get to the “this” keyword, you’ll notice some differences between the regular way of writing functions, or writing arrow functions!

References and further reading