Functions are one of the fundamental building blocks in JavaScript. A function is a block of code designed to perform a particular task in a repeatable way.
A function is in essence a mini-program inside of your program.
For example instead of writing
we could create a function that calculates the average of three numbers:
Functions can be assigned identifiers, just like literal values can be assigned to variables.
Using the identifier, a function can then be invoked whenever that code block is required.
To invoke a function, you append ()
to its identifier.
In this example, the code block is only one line long but it is easy to see that when working with long code blocks, functions are really useful.
Functions are defined using the function
keyword.
The above is called a function declaration, and creates a function called doSomething
that has access to a variable called parameter
Functions can also be created using a function expression:
This does essentially the same thing as the first example. However, using a function expression allows you to create anonymous functions: functions with no name. the above function is anonymous, because it is not given a name between the function
keyword and the parameter list.
Anonymous functions are useful when creating a function to pass as a parameter into another function, such as to an array enumerator.
The return
keyword can be used to stop the execution of the code in the function and provide a value back to the context in which the function was called.
In the above example, the return value of add
will be assigned to x
;
Any code in a function after a return statement will not be executed.
Parameters are values that get injected into the function as variables, taking their names/identifiers from the parameter name in the function definition.
So writing
is a bit like writing
Every function creates its own scope. This means that any variables created inside of a function (including the parameters), are not accessible outside of the function.
Functions can also be created using arrow syntax:
This is shorthand for the function expressions above. Arrow functions are a new addition to JavaScript. They are always anonymous.
The main benefit of arrow function is that they allow implicit returns for functions that only contains a return
statement:
is the same as