In JavaScript, a return value is the specific data or result that a function sends back after it has finished its task. When a function is called, it runs its code, and the return
statement specifies the value to be passed back to the part of the code that made the call.
Example 1: Returning a Simple String
// This function returns a simple greeting message.
function getGreeting() {
return "Hello, welcome to our JavaScript tutorial!";
}
// Call the function and store the returned value in a variable.
const message = getGreeting();
console.log(message); // Outputs: "Hello, welcome to our JavaScript tutorial!"
Explanation
The getGreeting
function is designed to do one thing: provide a string. The return
keyword sends that string back, allowing it to be stored in the message
variable and then displayed in the console.
Example 2: Returning a Number from a Calculation
// This function takes two numbers, adds them, and returns the result.
function addNumbers(a, b) {
return a + b;
}
// Call the function with two arguments and log the returned sum.
const sum = addNumbers(5, 10);
console.log(sum); // Outputs: 15
Explanation
This example shows how a function can perform a calculation and return the outcome. The addNumbers
function returns the result of a + b
, which is then assigned to the sum
variable.
Example 3: Returning a Boolean Value
// This function checks if a number is even and returns true or false.
function isEven(number) {
return number % 2 === 0;
}
// Use the function's return value in a conditional statement.
const numberToCheck = 10;
if (isEven(numberToCheck)) {
console.log(`${numberToCheck} is an even number.`); // This line will run.
}
Explanation
Functions are excellent for conditional logic. The isEven
function returns a boolean (true
or false
), which is perfect for use in if
statements to control the flow of your program.
Example 4: Returning an Object
// This function creates and returns a user object.
function createUser(name, age) {
return {
userName: name,
userAge: age
};
}
// The returned object is stored in the 'user' variable.
const user = createUser("Alex", 30);
console.log(user.userName); // Outputs: "Alex"
Explanation
A function can return complex data types like objects. The createUser
function returns a new object with properties based on the input arguments, making it a reusable way to create structured data.
Example 5: Returning an Array
// This function returns an array of numbers.
function getNumberSequence() {
return [10, 20, 30, 40, 50];
}
// The entire array is returned and stored in 'sequence'.
const sequence = getNumberSequence();
console.log(sequence[2]); // Outputs: 30
Explanation
Similar to objects, functions can also return arrays. This allows you to generate a list of items that can be stored in a variable and accessed by its index.
Example 6: Early Return from a Function
// This function returns a message, but exits early if the input is invalid.
function processInput(input) {
if (!input) {
return "No input provided. Please try again."; // Early return
}
return `Processing your input: ${input}`;
}
console.log(processInput()); // Outputs: "No input provided. Please try again."
console.log(processInput("data")); // Outputs: "Processing your input: data"
Explanation
The return
statement immediately stops the function's execution. In this example, if the input
is not provided, the function returns an error message and does not proceed to the final line.
Example 7: Returning a Function (Higher-Order Function)
// This function returns another function.
function createMultiplier(multiplier) {
return function(number) {
return number * multiplier;
};
}
// The returned function is stored in 'double'.
const double = createMultiplier(2);
console.log(double(5)); // Outputs: 10
Explanation
In advanced JavaScript, a function can return another function. This is a core concept of functional programming where createMultiplier
acts as a factory, creating and returning a new function that is then used to perform a calculation.