Functions

Functions define a set of instructions/statements that'll be performed.

Often with a given input, that'll transform to an output.

alt

Functions are a key aspect of JavaScript.

Declaring Functions

To use functions, you'll need to define or declare them

There are a few different ways to declare a function.

alt

The first one is with the function keyword, functions declared this way are hoisted.

JS

Copy

function sum(a, b) {
    return a + b;
}

console.log(sum(2, 3)); // 5

Functions can also be anonymous, meaning it doesn't have a name.

Such a function can be declared like this;

JS

Copy

const sum = function(a, b) {
    return a + b;
}

console.log(sum(2, 3)) // 5

This way of declaring a function, is called a function expression, it's worth noting function expression are not hoisted.

Calling Functions

To call a function, you'll use its name with () behind it, you can pass it's parameters inside the ()

JS

Copy

sum(2, 3)

When calling a function the parameters you pass to it are called arguments.