← Back to list

Why are Javascript functions considered as First-Class Citizens?

In our society, the term “first-class citizen” refers to individuals who enjoy freedom, respect, and a host of privileges that grant them a…

Deeksha Sareen · 2024-12-18 04:29 · 1 claps · 1.6 min read
#first-class-function #js-concepts #functional-programming #javascript #first-class-citizen
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development 🔒 · Cybersecurity 🌐 · Society · General 🧘 · Spirituality

Why are Javascript functions considered as First-Class Citizens?

In our society, the term “first-class citizen” refers to individuals who enjoy freedom, respect, and a host of privileges that grant them a favourable status. This concept can be likened to how certain elements within programming languages are treated with similar esteem. Specifically, in JavaScript, functions are regarded as first-class citizens. This means that JavaScript functions are uniquely versatile and can be utilized in ways similar to other core elements of the language.

Background

A programming language is described as having first-class functions if it grants functions the same rights and treatment as other first-class citizens. In essence, functions in JavaScript have the ability to be handled and manipulated like any other value within the language. When we say that functions are treated as first-class citizens in JavaScript, we recognize that they can:

  1. Be Treated as Values: Functions can be assigned to variables, allowing them to be stored and invoked later.
function add(a, b) {
    return a + b;
}

let sum = add;

let result1 = sum(2,3); //1. call like this
console.log(result1); // 5

//alternatively

let result2 = add(2,3); //2. call like this
console.log(result2); // 5
  1. Be Passed as Arguments to Other Functions: Functions can be passed to other functions as parameters. This is particularly useful in cases such as callback functions, where we want to execute a piece of logic after a certain operation.
function multiply(a, b) {
    return a * b;
}

function calculate(a, b, operation) {
    return operation(a, b);
}

let result = calculate(5, 10, multiply);
console.log(result); // 10
  1. Be Returned as Outputs of Other Functions: A function can return another function, allowing for flexible and dynamic behaviour in your code.
function createMultiplier(factor) {
    return function(num) {
        return num * factor;
    };
}

let multiplyBy2 = createMultiplier(2);
let result = multiplyBy2(5);
console.log(result); // 10

In summary, the concept of first-class citizens in JavaScript empowers developers by offering them rich functionality and flexibility. Functions can be treated like values, enabling a variety of programming techniques, such as callbacks and higher-order functions. This capability not only enhances code organization but also facilitates advanced programming paradigms, allowing developers to write more elegant and maintainable code.


메타데이터
post_id
68f6a8d74c50
slug
why-are-javascript-functions-considered-as-first-class-citizens-68f6a8d74c50
url
https://medium.com/@deekshasareen97/why-are-javascript-functions-considered-as-first-class-citizens-68f6a8d74c50
canonical_url
https://medium.com/@deekshasareen97/why-are-javascript-functions-considered-as-first-class-citizens-68f6a8d74c50
author_url
https://medium.com/@deekshasareen97
status
ok
fetched_at
2026-07-13 06:23:13