JavaScript Return Statement
In JavaScript, the return statement is used to send a result from a function back to the place where it was called. This allows you to use the result of a function in other parts of your code, making your programs more powerful and flexible. It’s one of the key features that helps organize code and makes it reusable.
Here are some common ways to use the return statement:
- Returning values: Pass a value from a function back to the caller so it can be used elsewhere.
- Improving code reusability: Functions with return values can be reused in different parts of your program.
- Working with calculations: Use return to send back results of mathematical operations or any function that performs a task.
What You'll Learn
You will learn the basics of the return statement in JavaScript, including how to return values from functions and how this can be useful for simplifying your code. We’ll cover basic examples first and move on to more complex usage in later lessons.
Understanding the return Statement
The basic syntax for the return statement is:
function functionName() {
// code
return expression;
}
function functionName() {
// code
return expression;
}
- expression: This is the value or result that you want to return from the function. It could be a variable, a calculation, or a literal value like a number or string.
The return statement ends the function and sends the specified result back to the caller. Once the function returns a value, it can be used anywhere that the function was called, like assigning it to a variable or directly printing it out.
Example 1: Returning a Simple Value
In this example, we’ll create a function that simply returns a message when called:
function greet() {
return "Hello, World!";
}
console.log(greet());
function greet() {
return "Hello, World!";
}
console.log(greet());
How It Works:
- greet: This is a simple function that returns the string "Hello, World!".
- return: The return statement sends the string back to where the function was called.
- console.log(greet()): The result of calling greet() is printed to the screen, which shows "Hello, World!".
Output
Hello, World!
Hello, World!
Example 2: Returning a Calculation Result
Functions can also return the results of calculations. Here's an example of a function that returns the sum of two numbers:
function addNumbers(a, b) {
return a + b;
}
let result = addNumbers(3, 5);
console.log(result);
function addNumbers(a, b) {
return a + b;
}
let result = addNumbers(3, 5);
console.log(result);
How It Works:
- addNumbers: This function takes two parameters, a and b, and returns their sum.
- return a + b: The return statement returns the result of adding a and b.
- let result = addNumbers(3, 5): The returned value is stored in the variable result.
- console.log(result): The value stored in result (which is 8) is printed to the screen.
Output
8
8
Example 3: Returning Multiple Values
In JavaScript, a function can return multiple values as an array or an object. Here’s an example:
function getCoordinates() {
return [10, 20];
}
let [x, y] = getCoordinates();
console.log(x, y);
function getCoordinates() {
return [10, 20];
}
let [x, y] = getCoordinates();
console.log(x, y);
How It Works:
- getCoordinates: This function returns two values as an array.
- return [10, 20]: The function sends back both values, which can be unpacked into separate variables.
- let [x, y] = getCoordinates(): The two returned values are unpacked into x and y.
- console.log(x, y): The values of x and y (10 and 20) are printed to the screen.
Output
10 20
10 20
Exercises
Try out the following exercises to practice using the return statement.
1. Write a function that returns your name.
// Exercise 1: Return your name
function getName() {
return "Your Name";
}
console.log(getName());
// Exercise 1: Return your name
function getName() {
return "Your Name";
}
console.log(getName());
2. Write a function that returns the square of a number.
// Exercise 2: Return the square of a number
function squareNumber(number) {
return number * number;
}
console.log(squareNumber(4));
// Exercise 2: Return the square of a number
function squareNumber(number) {
return number * number;
}
console.log(squareNumber(4));
3. Write a function that returns the maximum of two numbers.
// Exercise 3: Return the maximum of two numbers
function maxOfTwo(a, b) {
return a > b ? a : b;
}
console.log(maxOfTwo(10, 20));
// Exercise 3: Return the maximum of two numbers
function maxOfTwo(a, b) {
return a > b ? a : b;
}
console.log(maxOfTwo(10, 20));
*Tip: After completing an exercise, experiment with returning different types of data, like strings, arrays, or even objects!
Frequently Asked Questions
What is the return statement in JavaScript?
What is the return statement in JavaScript?
The return statement in JavaScript is used inside a function to send a value back to the place where the function was called. It also ends the function's execution.
Can every JavaScript function use the return statement?
Can every JavaScript function use the return statement?
Yes, any function in JavaScript can use the return statement, but it’s optional. If no return statement is used, the function returns undefined
by default.
What happens if you return multiple values in JavaScript?
What happens if you return multiple values in JavaScript?
JavaScript functions can't return multiple values directly. Instead, you can return an array or an object to group multiple values.
What is the difference between console.log
and return
in JavaScript?
What is the difference between console.log
and return
in JavaScript?
console.log
prints messages to the browser console, mostly for debugging. return
is used in functions to send a value back to the caller.
Does return
stop a function in JavaScript?
Does return
stop a function in JavaScript?
Yes. When return
is executed in a function, the function stops immediately and returns the specified value.
What's Next?
Next, you'll explore the concept of variable scopes in JavaScript. Understanding how block, function, and global scopes work will help you manage variable accessibility and avoid issues like variable collisions in your code.