Understanding the Different Types of Functions in JavaScript: A Comprehensive Guide for Beginners

Ashwini Paraye
5 min readMay 8, 2023

--

In JavaScript, functions are one of the most important concepts, allowing developers to create reusable code blocks that can perform various tasks. Functions are like machines that take inputs, process them, and produce outputs. JavaScript has several types of functions, each with its own unique properties and uses. Let’s get started!

Named functions (function declaration) :

  • In JavaScript, a named function is simply a function that has a name.
  • When you create a named function, you give it a specific name so that you can refer to it later in your code.
  • This is useful when you want to reuse the same function multiple times, or when you want to call the function from another part of your code.
  • Named functions are hoisted (loaded into memory at compilation).
function sayHello(name) {
console.log("Hello, " + name + "!");
}

Anonymous functions (function expression) :

  • An anonymous function in JavaScript is a function that doesn’t have a name.
  • It’s just like a regular function, except that it doesn’t have a specific identifier to reference it by. Instead, it’s usually assigned to a variable or passed as an argument to another function.
  • An anonymous function cannot be accessed after it is created; it can only be retrieved by a variable in which it is stored as a function value.
  • Anonymous functions can be very handy when developing IIFEs (Instantly Invoked Function Expressions).
var sum = function(a, b) {
return a + b;
};

Arrow functions :

  • Arrow functions in JavaScript are a way to write shorter and more concise functions.
  • They are created using the => operator and do not require the function keyword or the return keyword (if there is only one expression).
  • Arrow functions are anonymous functions i.e. they are functions without a name and are not bound by an identifier.
  • Arrow functions do not return any value and can be declared without the function keyword.
const multiply = (a, b) => a * b;

Higher-order functions :

  • Higher-order functions are functions in JavaScript that can take other functions as arguments, or can return functions as their output. In other words, they treat functions as values.
function multiplyBy(num) {
return function(x) {
return x * num;
}
}
  • In this example, multiplyBy is a higher-order function because it returns a new function. The returned function takes a single argument x and multiplies it by the num parameter passed to multiplyBy.

First-Order Functions :

  • First-order functions in JavaScript are functions that do not take any other functions as arguments or return a function as their result. They are the simplest type of functions in JavaScript.
  • In JavaScript, functions are treated as a first-class citizens, which means that functions are just another type of object, and they can be used in the same way as other values like strings, numbers, and objects.
  • This means that you can assign a function to a variable, just like you can assign a value to a variable.
function greet(name) {
console.log('Hello, ' + name + '!');
}

const sayHello = greet;
sayHello('John'); // Output: "Hello, John!"

Pure Functions :

  • A Pure Function is a function (a block of code) that always returns the same result if the same arguments/input are passed.
  • It Doesn’t depend on anything outside of the function to work (like global variables) and doesn’t change anything outside of the function (like updating a database)
  • Also, a pure function does not produce any observable side effects such as network requests or data mutation, etc.
function addNumbers(a, b) {
return a + b;
}
  • This function takes two numbers as arguments and returns their sum. It always returns the same output for the same input values, and it does not modify any variables outside of its scope or have any other side effects.

Impure Functions :

  • In JavaScript, an impure function is a function that produces side effects and/or has an output that is not solely determined by its input parameters.
  • A side effect is any change that a function makes outside of its own scope, such as modifying a global variable, logging to the console, updating the DOM or making network requests.
  • An impure function may also rely on state that is not passed in as a parameter, such as a global variable or the current time.
let count = 0;

function incrementCount() {
count++;
return count;
}

Callback Functions :

  • A callback function is a functions that is passed as an argument to another function, and is called after the main function has finished its execution.
  • The main function is called with a callback function as its argument, and when the main function is finished, it calls the callback function to provide a result.
  • Callbacks are commonly used in JavaScript for asynchronous operations like making network requests or processing large amounts of data, where you don’t want to block the main thread while waiting for the operation to complete.
  • Instead, you pass a callback function that will be called when the operation is complete, allowing your code to continue executing in the meantime.
function printMessage(message) {
console.log(message);
}

function doSomething(callback) {
console.log("Doing something...");
callback("Callback executed!");
}

doSomething(printMessage);

// Output:
// "Doing something..."
// "Callback executed!"

Recursive Functions :

  • Recursive functions in JavaScript are functions that call themselves.
  • Recursive functions are commonly used in programming to solve problems that can be broken down into smaller, similar sub-problems.
  • The idea behind recursive functions is to break a problem down into smaller and smaller sub-problems until they become simple enough to solve directly.
function factorial(n) {
if (n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

In conclusion, functions are a powerful tool in JavaScript that allows developers to create reusable code blocks. The different types of functions have their own unique properties and uses, and it’s important to choose the right type of function for each task. By understanding the different types of functions in JavaScript, developers can create more efficient and effective code.

I hope you found this article helpful. Thank you for reading! 😀

Happy Coding !

--

--

Ashwini Paraye

Fullstack Developer | 👨‍💻 Writing about programming concepts | React.js | JavaScript | Vue.js | Golang