Functions in JavaScript

Function code

What is a Function?

Firstly, let's talk about what the word 'function', actually means.

A function in simple terms is a process put in place to complete a task. This can be as simple as a function that adds two numbers together!

Functions in JavaScript

Let's talk about the syntax of a function in JavaScript.

Take this block of code and we will break it down, bit by bit together.

function addTogether(num1, num2) {
return num1 + num2;
};

This block of code is very simple and does a simple process.

Firstly we are starting with the keyword 'function'. This is telling JavaScript that we are wanting to write out a function.

Secondly, the name of the function comes into play, in this case it is 'addTogether'. The name is camel-cased which means that the first word starts with no capital letter and every word after has a capital letter to increase readability.

Then comes the parenthesis with 2 parameters, "num1" and "num2". These are the names we will use within the function to refer to the arguments that are passed into the function when it is called.

In the function body is the 'return' statement. This is used to stop the execution of a function and returns a value from it. In this case it would be the 2 arguments added together. The body of the function is surrounded by curly braces { }.

Anything below the return statement is not executed.

addTogether(2, 3);
// Returns the number literal 5

This has been functions in simple terms, I hoped this helped!

Take care.

Join the mailing list!