INTRODUCTION
JavaScript functions are blocks of code that perform a specific task or calculation. They are a fundamental building block of JavaScript programming and play a crucial role in creating dynamic and interactive web applications.
Functions can also be assigned to variables, allowing them to be treated as first-class objects in JavaScript. This enables functions to be passed as arguments to other functions or returned from functions, supporting powerful functional programming techniques.
const Greet = function (name) {
return 'Hello, ' + name + '!';
};
console.log(Greet('raymond'));
//result
Hello, raymond
What Is Higher-order Function
Higher-order function is a concept in programming where a function accepts another function as an argument and returns it's function as the result. In other words treats other functions as first-class citizens allowing them to be used just like other data type, like string or number. A nice example of higher-order function is the setTimeOut function, That schedules a particular function to execute after a specified time delay.
function sayHello() {
console.log("Hello!");
}
setTimeout(sayHello, 1000); // After 1 second, "Hello!" will be printed
Other examples of higher-order function are reduce, sort, find, some, forEach etc.
Higher-order functions are flexible concept, and they enable several useful programming patterns, such as functional programming. By accepting or returning functions, higher-order functions enable code reusability, modularity, and the separation of concerns.
Code Example
const oneWord = function (str) {
return str.replace(/ /g, '-').toLowerCase();
};
const upperFirstWord = function (tri) {
const [first, ...others] = tri.split(' ');
return [first.toUpperCase(), ...others].join(' ');
};
//HIGHER-ORDER FUNCTION(A FUNCTION THAT RECEIVES ANOTHER FUNCTION AS AN ARGURMENT,THAT RETURNS A NEW FUNCTION OR BOTH) CALL BACKS FUNCTIONS
const transform = function (fri, fn) {
console.log(`Original String: ${fri}`);
console.log(`Translated String: ${fn(fri)}`);
console.log(`Transformed by: ${fn.name}`);
};
transform(`Javascript is the best!`, upperFirstWord);
transform('javascript is the best!', oneWord);
Let's go through each line of code and explain its purpose
const oneWord = function (str) {
return str.replace(/ /g, '-').toLowerCase();
};
onWord function receives str as an argument
Inside the function, it uses the 'replace' method with a
regular expression '/ /g' to replace all spaces with hyphens '-'The 'toLowerCase' method is then used to convert the entire
string to lowercase.
const upperFirstWord = function (tri) {
const [first, ...others] = tri.split(' ');
return [first.toUpperCase(), ...others].join(' ');
};
The upperFirstWord function takes a string tri as an argument.
Inside the function, it uses the split method to split the input
string into an array of words, using a space ' ' as the separator.The destructuring assignment [first, ...others] is used to extract the first word from the array and store the remaining words in the others array.
The toUpperCase method is used to convert the first word to uppercase.
The join method is then used to combine the modified first word with the remaining words, separated by spaces.
const transform = function (fri, fn) {
console.log(`Original String: ${fri}`);
console.log(`Translated String: ${fn(fri)}`);
console.log(`Transformed by: ${fn.name}`);
};
Here the transform function can be referred as the higher-order function
The transform function takes two arguments: fri and fn.
fri is expected to be a string, and fn is expected to be a function that can transform the string.
Inside the function, it first logs the original string (fri) to the console.
It then invokes the function fn (which should be a transformation function) with fri as the argument and logs the transformed result to the console.
Finally, it logs the name of the transformation function (fn) to the console.
transform(`Javascript is the best!`, upperFirstWord);
//results
Original String: Javascript is the best!
Translated String: JAVASCRIPT is the best!
This line calls the transform function.
It passes the string "Javascript is the best!" as the first argument (fri) and the function upperFirstWord as the second argument (fn).
The transform function will then log the original and transformed strings to the console, along with the name of the transformation function.
transform('javascript is the best!', oneWord);
//results
Original String: javascript is the best!
Translated String: javascript-is-the-best!
This line calls the transform function again, but this time with different arguments.
It passes the string 'javascript is the best!' as the first argument (fri) and the function oneWord as the second argument (fn).
The transform function will once again log the original and transformed strings to the console, along with the name of the transformation function
In conclusion, higher-order functions are powerful constructs in programming languages that enable functions to be treated as first-class citizens. They can accept other functions as arguments and return functions as results. This flexibility allows for code modularity, reusability, and separation of concerns, making higher-order functions a fundamental tool for writing concise, maintainable, and versatile code.
Top comments (0)