Skip to content

Functions

Functions are separate code blocks that are designed to perform specific tasks. They are created using the keyword function. Creating functions increases the clarity of the code and makes programming easier and allows you to execute the same set of instructions many times without having to write the same code every time. The function is called by another part of the script, and when it is called, the code contained inside is executed.

The function declaration and its call looks like this:

function myFunction() {
    // function code
}

myFunction(); // function call

The name of the function should meet the same requirements as variable name.

Function declaration

In JavaScript, functions can be declared in several ways:

  • using the keyword function
  • with the so-called arrow function, using =>.

An example of a function declaration in JS using function is as follows:

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

Function parameters

To functions you can pass arguments (sometimes called parameters) or values that can affect the function or be processed by the function. Parameters are passed by writing them between brackets after the function name, and individual parameters are separated from each other by a comma"

function someFunction(param1, param2) { // function has 2 arguments - param1 and param2
    // function body
}

someFunction(10, 20); // function call

Return

The function can return a value. By using the return instruction, we can command the function to return a certain value. This instruction simultaneously interrupts further operation of the function and causes the return of the value occurring after the return. The use of the return instruction itself without any arguments interrupts the function.

Example:

function isEven(value) {
  if (value % 2 === 0) {
    return 'Value is even';      
  }
  return 'Value is odd';
}

const output = isEven(10); // result - 'Value is even'.


Functions can also be anonymous, which means that a function can be called at the place where we declare it. For this purpose, the whole declaration should be in parentheses, and then in the following parentheses we provide a list of arguments, e.g:

(function(a, b) {
    return a + b;
})(10, 20); // 30

In JS, each expression can be assigned to a variable. This is no different with functions that also allow it. After assigning such a function to a variable, we can call it using the name of that variable, e.g:

const multiply = function(a, b) {
    return a * b;
}

const result = multiply(2, 3); 
console.log(result); // 6 will be displayed on the console

Arrow function is another way of creating it. They have a slightly shorter declaration than those created using the function. Their syntax consists of an argument list, an arrow (=>) and a body, for example:

const divide = (dividend, divisor) => dividend / divisor;
const result = divide(5, 2);

console.log(result); // 2.5
const divideAndLogResult = (dividend, divisor) => {
  const result = dividend / divisor;
  console.log("Division result: " + result);
  return result;
}

divideAndLogResult(10, 2);

As you can see, in these examples:

  • if the arrow function consists of one expression, the keyword return is not required
  • if arrow function consists of multiple expressions, these expressions must be in the code block, inside the brackets. If the function returns an object, the keyword return is required.