What Are Functions in JavaScript? (Example Included)

Table of Contents

A function in JavaScript is code that performs a job and is usually activated when something invokes it.

Here’s an example of a typical function:

function addThreeNumbersTogether(n1, n2, n3) {
   return n1 + n2 + n3;

addThreeNumbersTogether is the name of the function you’ve created and this function returns the sum of these 3 numbers.

One thing to take note of is that a function does not have a semicolon at the end of it.

The parts of a function

The name of a function is the word that comes right after the word “function.”

The variables inside the parentheses are each called a parameter (n1, n2, n3). n1 is a parameter, n2 is another parameter, and n3 is the last parameter.

We provide the numbers which are also known as arguments, which are the values of each one of the parameters.

So for example, invoking the function above would look something like this:

addThreeNumbersTogether(1, 3, 6);

The return here is:


Leave a Comment