javascript

Functions as Values


Functions can be treated just like any other value, such as strings or numbers.


Functions as values provide flexibility and enable powerful programming paradigms like functional programming. They allow for higher-order functions, which are functions that can accept other functions as arguments or return functions as results

 

Example:

// Function assigned to a variable
var hello = function (name) {
		console.log("Welcome, " + name + "!");
	};

// Function passed as an argument to another function
function funcCall(func, name) {
	func(name);
}

funcCall(hello, "Rohit"); // Output: Welcome, Rohit!

 

The greet function is assigned to a variable and then passed as an argument to funcCall(), where it is called with the name "Rohit".