![]() |
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() Table of Content Simple return statementReturn statement is used to depict how a function after the completion of its execution can produce some result that can be used further. Example: Sum of two numbers returned as simple return statement. Javascript
Output
3 Return statement to return different typesFunctions in JavaScript are not just limited or bounded to return specific data types, but they can return different data types based on the logic that is defined inside the function. Example: Demonstration of returning string, number & array data type. Javascript
Output
Returned value is GeeksforGeeks Returned value is 2015 Retunred value is 1,2,3,4,5 Return statement for returning objectsJavaScript functions also allow us to return the object which helps in hiding the related data and behaviours. Example: Returning company object using the return statement. Javascript
Output
{ name: 'GeeksforGeeks', founded: 2009, location: 'Noida' } Return statement to return a functionJavaScript 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
Output
GeeksforGeeks Return statement to exit function prematurelyA return statement can also be used to exit the function prematurely in JavaScript in those cases where the condition is met. Example: Using a return statement to early exit from the function based on certain conditions. Javascript
Output
Number is divisble by 2: false Number is divisble by 2: true |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |