Introduction
In this lab, we will learn about the callOrReturn function, which is a useful utility function in JavaScript. This function can be used to determine whether the given argument is a function or not. If the argument is a function, it will be called with the rest of the arguments. Otherwise, it will simply return the given argument. By the end of this lab, you will be able to use this function to simplify your code and improve its readability.
A Function That Calls or Returns Another Function
To begin practicing coding, open the Terminal/SSH and type node.
Here's a function called callOrReturn that takes an argument and calls it if it's a function, otherwise, it returns it. Here's how it works:
- The function takes two parameters:
fnand...args.fnis the argument to be checked, and...argsis the list of arguments to be passed to the function if it's called. - It uses the
typeofoperator to check if the given argument is a function. - If the argument is indeed a function, it calls the function using the spread operator (
...) to pass the rest of the given arguments. Otherwise, it simply returns the argument. - Here's an example of how to use the
callOrReturnfunction:
const callOrReturn = (fn, ...args) =>
typeof fn === "function" ? fn(...args) : fn;
callOrReturn((x) => x + 1, 1); // 2
callOrReturn(1, 1); // 1
In the first example, callOrReturn(x => x + 1, 1) calls the function x => x + 1 with the argument 1, which returns 2. In the second example, callOrReturn(1, 1) simply returns 1 since it's not a function.
Summary
Congratulations! You have completed the Call or Return lab. You can practice more labs in LabEx to improve your skills.