From the course: Learning Functional Programming with JavaScript (ES5)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Closure and returning functions

Closure and returning functions - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript (ES5)

Start my 1-month free trial

Closure and returning functions

- [Instructor] We've already seen that functions can be assigned to variables and, like variables can be passed as arguments to other functions. As it happens, this means that we can also return functions from other functions. This concept when combined with assigning functions to variables, and passing functions as arguments, can enable us to write some incredibly flexible code. This is a simple example showing JavaScript's basic syntax for returning functions. Syntax-wise, returning functions really is just like returning values or other variables. Notice that since putting parentheses after a function name transforms it into its return value, we can do this as well. While this may look a little weird, it's perfectly valid syntax in JavaScript. The first set of parentheses transforms give me a function into its return value, which in this case is another function. The next set of parentheses transforms this other function into its return value, in the process logging a message to…

Contents