How to use Return in JavaScript ?
source link: https://www.geeksforgeeks.org/how-to-use-return-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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
}
Simple return statement
Return 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
function sum(firstNumber, secondNumber) { return firstNumber + secondNumber; } console.log(sum(1, 2)) |
3
Return statement to return different types
Functions 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
// Returning string data type function Value() { return "GeeksforGeeks" ; } // Returning number data type function Number() { return 2015; } // Returning array function Array() { return [1, 2, 3, 4, 5]; } console.log(` Returned value is ${Value()} \n`); console.log(` Returned value is ${Number()} \n`); console.log(` Retunred value is ${Array()} \n`); |
Returned value is GeeksforGeeks Returned value is 2015 Retunred value is 1,2,3,4,5
Return statement for returning objects
JavaScript 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
function Object(name, founded, location) { let company = { name: name, founded: founded, location: location } return company } let companyobject = Object( "GeeksforGeeks" , 2009, "Noida" ) console.log(companyobject) |
{ name: 'GeeksforGeeks', founded: 2009, location: 'Noida' }
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 "GeeksforGeeks" ; } } console.log(Function()()); |
GeeksforGeeks
Return statement to exit function prematurely
A 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
function isDivisible(number) { if (number%2==0) { return true ; } return false ; } console.log(` Number is divisble by 2: ${isDivisible(107)}`) console.log(` Number is divisble by 2: ${isDivisible(24)}`) |
Number is divisble by 2: false Number is divisble by 2: true
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK