Table of contents


In JavaScript you can create functions to write reusable blocks of code with functionality similar to most other programming languages, although, because of the nature of JavaScript, they might behave a bit differently, depending on the circumstances and the way you declare them.

Function declaration

In its simplest form a custom JavaScript function is declared as follows.

function myFunction() {

}

You can then call (use) this function later in your code with:

myFunction();

Now as it is, our function doesn’t actually do anything. So let’s add some functionality and delve into the subject of arguments and parameters.

Arguments and parameters

function myFunction(number1, number2) {
    console.log(number1 + number2);
}

The functionality we’ve added here is simply logging to the console the result of number1 + number2. As you can see these variables aren’t declared or defined anywhere else but between the function parenthesis. This is what are called parameters.

Parameters are variables entered as a part of the function definition. And these are the types of values we expect to be inserted when our function is called, like so:

myFunction(1,2);

The numbers 1 and 2 are passed into the parenthesis of the function when it is called, and are called arguments .

#function #javascript #this #custom-functions

Custom JavaScript Functions
1.25 GEEK