Skip to content

Functions

Functions are dedicated blocks of code that are designed to perform specific tasks. They are created using the function keyword. Creating functions increases the clarity of the code and facilitates programming, and allows you to repeatedly execute the same set of instructions 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 in it is executed.

The function declaration and its call looks like this:

function myFunction() {
    // function code
}

myFunction(); // function call

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

Function declarations

In JavaScript, functions can be declared in several ways:

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

An example function declaration in JS using function looks like this:

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

Function parameters

Functions can be passed arguments (sometimes called parameters) or values that can affect the operation of the function or be processed by the function. Parameters are passed by writing them between the brackets after the function name, and the individual parameters are separated with a comma, i.e.

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

someFunction(10, 20); // function call

Return

A function may return some value. By using the return statement, we can instruct the function to return a specific value. At the same time, this statement interrupts further operation of the function and returns the value following return. Using the return statement alone with no arguments causes the function to be aborted.

For example:

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

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


Functions can also be anonymous, i.e. the function can be called at the place where it is declared. For this purpose, the entire declaration should be enclosed in parentheses, and then in the following parentheses we give a list of arguments, for example:

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

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

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

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

Arrow function, the so-called arrow function , is another way to create it. They have a slightly shorter declaration than those created with 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 the examples above:

  • if the arrow function consists of a single expression, the 'return` keyword is not required
  • if the arrow function consists of multiple expressions, these expressions must be placed in a code block, inside braces. If the function does return a certain object, the return keyword is required.