"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Functions as First-Class Citizens in JavaScript

Functions as First-Class Citizens in JavaScript

Published on 2024-11-02
Browse:187

Functions as First-Class Citizens in JavaScript

What is a First-Class Function?

First-Class Citizens’ means that functions can be used in the same way as other data types. It implies that functions can be assigned to variables, passed as arguments to other functions, and returned as values. This is a crucial concept in functional programming as it allows us to write more modular and reusable code.

  • Assigned to variables
  • Passed as arguments to other functions
  • Returned as values from functions

Here are some examples of using functions as first-class citizens in JavaScript:

  • Assigning Functions to Variables: You can assign functions to variables and use the variables as you would with any other variable.

Example:

const add = function(x, y) {
  return x   y;
}
console.log(add(5, 4)); // Output: 9
console.log(typeof(add)); // Output: function
  • Passing Functions as Arguments: You can pass functions as arguments to other functions. This is useful for designing higher-order functions or callback functions.

Example:

function greet(name, callback) {
  const message = "Hello, "   name   "!";
  callback(message);
}

function logMessage(message) {
  console.log(message); // Logs "Hello, Nozibul!"
}

greet("Nozibul", logMessage); // Logs "Hello, Nozibul!"
  • Returning Functions as Values: A function can return another function from within. This is useful for creating functions that can be used in subsequent operations, such as currying functions.

Example:

function add(x) {
  return function(y) {
    return x   y;
  };
}

const addFive = add(5);
console.log(addFive(3)); // Output: 8
  • Storing Functions in Arrays: Functions can be stored in arrays just like any other value.

Example:

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

var arr = [];
arr.push(add);
console.log(arr); // Output: [ [Function: add] ]
console.log(arr[0](2, 5)); // Output: 7
  • Storing Functions in Objects: Functions can be stored as properties of objects.

Example:

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

var obj = {
  sum: add
};

console.log(obj.sum(5, 7)); // Output: 12
console.log(obj); // Output: { sum: [Function: add] }

These examples demonstrate how functions in JavaScript can be treated as first-class citizens, allowing for powerful functional programming paradigms.

Release Statement This article is reproduced at: https://dev.to/nozibul_islam_113b1d5334f/functions-as-first-class-citizens-in-javascript-4fji?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3