Return statement to return a function
JavaScript has a concept of higher-order functions that allows us to pass another function as an argument inside a function or return another function or both, this mechanism can be used for creating another reusable function with specific behaviour and also allows the creation of closure.
Example: Using a return statement to return a function.
Javascript
function Function() { return function () { return "w3wiki" ; } } console.log(Function()()); |
Output
w3wiki
How to use Return in JavaScript ?
JavaScript allows us to use the return statement to end the function’s execution to specify the value that needs to be returned to the code that is calling it. Below are the methods and examples of how to use a return statement in JavaScript.
Syntax:
function nameOfFunction()
{
return result // Value that need to be returned
}
Table of Content
- Simple return statement
- Return statement to return different types
- Return statement for returning objects
- Return statement to return a function
- Return statement to exit function prematurely
Contact Us