Table of Contents
The `greet` function takes no arguments, and when called, it simply prints a greeting to the console. Functions can also take arguments and use them to perform operations:
In this case, the `greet` function takes one argument: `name`. When we call the function, we provide the argument, which the function then uses to print a customized greeting.
While the above functions perform an action, they do not provide any result that can be used later in the code. This is where the `return` statement comes into play. The `return` statement ends function execution and specifies a value to be returned to the function caller.
ES6 introduced a new way of writing concise functions, called arrow functions. They provide a shorter syntax than function expressions and do not rebind `this`.
Arrow functions also simplify functions that take arguments or return a value:
Here’s an example of a higher-order function that takes a function as an argument:
In this example, `greet` is a higher-order function that takes a function `formatter` as an argument. `upperCaseName` is passed as an argument, which converts the name to uppercase.
Here’s an example using the `setTimeout` function, which executes a callback function after a specified delay:
Here’s an example of a closure: