A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/are-functions-objects-in-javascript/ below:

Are functions objects in javascript?

Are functions objects in javascript?

Last Updated : 19 Nov, 2024

Yes, Functions are considered first-class objects, which means they have the same capabilities as other objects. The following are examples that demonstrates functions behaving as objects:

Can be assigned to variable

JavaScript
// Assign a function to a variable
const x = function() {
    return `GfG!`;
};

// Call the function with the variable name
console.log(x()); // Outputs: GfG!

Can be passed to other functions

JavaScript
function fun1() {
    return `GfG`;
};

function fun2(fun) {
    console.log(fun());
}

fun2(fun1); 

Can be returned from functions

JavaScript
function getFun(x) {
    return function(y) {
        return x * y;
    };
}

const res = getFun(2);
console.log(res(5)); 

Can have properties and methods

JavaScript
// Define a function that adds two numbers
function add(a, b) {
    return a + b;
}

// Add a custom property to the function
add.description = "Adds two numbers.";

console.log(add(3, 4));          
console.log(add.description);   

Output
7
Adds two numbers.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4