Functions in JavaScript

Functions in JavaScript

A function is a block of organized, reusable code that is used to perform some action. There are multiple ways to define functions in JavaScript. Here we will look at function declarations and function expressions.

Function Declaration

The standard way of defining a function in JavaScript is a function declaration, also called function definition or function statement.

It consists of the function keyword, the name of the function, and a comma-separated list of parameters in round brackets. This is followed by the function body (collection of statements that defines what a function does) wrapped in curly brackets.

function someName(param1, param2, param3) {
  // ...
}
        

In JavaScript, a function is invoked (called) by stating the function name followed by parentheses that contain the arguments.

someName(arg1, arg2, arg3);
        

Parameters

When working with parameters inside the function body, be aware of possible side effects.

  • Values of primitive data types are immutable. The original value is never affected by what happens to the argument in the function body.
  • For all other values (objects, arrays, functions), a reassignment will not affect the original value. However, if you modify such an argument (e.g. add a key to an object), that also modifies the original value that was passed in.

By default, all parameters defined in the function declaration are optional in JavaScript. If you provide fewer arguments than there are parameters, the missing arguments will be undefined inside the function. In many cases, it makes sense to assign a more appropriate default value than undefined. This can be done by specifying default parameters directly in the function definition.

function someName(param1 = defaultValue1, param2 = defaultValue2) {
  // ...
}
        

Return Statement

A return statement ends the function execution and specifies a value to be returned to the function caller. A function can have multiple return statements.

function checkNumber(num) {
  if (num === 0) {
    return 'You passed 0, please provide another number.';
  }

  return 'Thanks for passing such a nice number.';
}
        

The result of a function that returns no value or does not have a return statement is undefined. There are no implicit returns in JavaScript.

function nakedReturn(a) {
  a * 2;
  return;
}

nakedReturn(1);
// => undefined

function noReturn(a) {
  a * 2;
}

noReturn(1);
// => undefined
        

In JavaScript, you can only return exactly one value. If you want to pass more information, you need to combine it into one entity first, usually into an object, or an array.

function divide(a, b) {
  return {
    quotient: Math.floor(a / b),
    remainder: a % b,
  };
}
        

Function Expression

A function declaration is a standalone statement. But sometimes it is helpful to define a function as part of another expression, e.g., in an assignment, as a function parameter (callback) or as value in an object. This can be done with a function expression. It has the same syntax as a function declaration, only that the function name can be omitted to create an anonymous function.

const someFunction = function (param) {
  // ...
};

someOtherFunction(function (param) {
  // ...
});

const obj = {
  someFunction: function (param) {
    // ...
  },
};
        

Learn More

To view or add a comment, sign in

More articles by Zeeshan Safdar

  • Regular Expressions in JavaScript

    A Regular Expression (or Regex) is a sequence of characters that we can use to target and manipulate certain elements…

    1 Comment
  • Prototypes & Classes in JavaScript

    JavaScript includes the capabilities for object-oriented programming (OOP). In OOP, you want to create objects…

  • Strict Mode in React

    React, being a popular JavaScript library for building user interfaces, offers a powerful feature called to enhance…

  • Sets in JavaScript

    In JavaScript, a set is a list-like structure containing unique values, which can be primitives and/or object…

    2 Comments
  • Arrow Functions in Javascript

    Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a…

    2 Comments
  • Promises in JavaScript

    The object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. The…

  • Array Destructuring | Rest & Spread in JavaScript

    About Array Destructuring Array [destructuring assignment][array_destructuring_docs] is a concise way of extracting…

    2 Comments
  • JavaScript Array Transformations

    In JavaScript, the Array class has many powerful built-in methods for transforming arrays. These methods make it much…

  • Numbers in JavaScript

    There are two different kinds of numbers in JavaScript - numbers and "bigints" Numbers are the most used, and represent…

  • JavaScript Type Conversion

    In JavaScript, values may be of different types. Changing the type of a variable can be done by explicit type…

    2 Comments

Insights from the community

Others also viewed

Explore topics