1. Home
  2. Articles
  3. javascript

4 Tips for Properly Using the Return Statement in Your Code


3 min read

The return statement is an often overlooked programming paradigm. It allows you to specify a value to be returned by a function and signals the end of its execution. Using this statement properly can ensure your code runs smoothly and may prevent unexpected bugs. Here are five tips for using the return statement effectively:

1. Use the return statement to end a function or method

The return statement signals the end of a function or method. When the return statement is encountered, the function immediately terminates:

1function sayMyName(name) {
2 console.log(name); return; // end the function

It's worthwhile noting that the return statement is affected by ASI Automatic Semicolon Insertion. This means that you cannot create a new line after the return statement is called:

1/* Incorrect - would return nothing and flag an error. */
2return; name;
4/* Correct - would return the value of name. */
5return ( name; );

2. Return a value of the correct data type

When using the return statement, make sure to return the correct data type of the intended value. If you fail to return the correct data type, your program will behave unexpectedly and may result in errors. This is due to a feature of JavaScript known as Type Coercion.

Look out for my articles on Using TypeScript and Getting Started with JavaScript Unit Testing to learn more about how to work around this pitfall confidently.

1function add(a, b) {
2 return a + b; // correct - returns a number
5function subtract(a, b) {
6 return a - b; // correct - returns a number
9function multiply(a, b) {
10 return `${a} times ${b} is equal to: ${a * b}`; // incorrect - returns a string

3. Return a value that is appropriate for the function

When writing functions, it is essential to consider what information you need from your data and how it can affect your code. For example, if your function needs to calculate the average of a list of numbers, returning the sum of those numbers would not be correct.

Make sure you carefully consider what information you need when writing functions and how it can affect your code.

1function average(numbers) {
2 let total = 0;
3 for (let number of numbers) {
4 total += number;
5 }
7 return total; // incorrect - returns the sum of the numbers, not the average
10function average(numbers) {
11 let total = 0;
12 for (let number of numbers) {
13 total += number;
14 }
16 return total / numbers.length; // correct - returns the average of the numbers

When writing a function, it's important to be intentional about the following:

  1. What the function is intended to do
  2. What data the function is intended to return
  3. Is the data returned immutable?

4. Use multiple return statements if necessary

Sometimes, you may want to return a value immediately if certain conditions are met. This would apply to using if statements, as well as switch statements.

1function options(option) {
2 switch (option) {
3 case "DnD":
4 return "do-not-disturb";
5 case "Silent":
6 return "silent-mode";
7 default:
8 return "no-option-selected";
9 }
12function getOptions(option) {
13 if (option === "DnD") {
14 return "do-not-disturb";
15 }
17 return option;

In the options function above, we can omit the break keyword, along with a temporary variable to store the value if we explicitly return within a case - this makes code more readable and saves memory.

In the getOptions function, we can also always ensure that we return a value regardless of whether a condition is met. If the condition is true, it will execute the return statement within the if - if not, it simply ignores that statement and returns the value of option

By following these tips, you can use the return statement effectively and ensure that your code runs smoothly and correctly. Proper use of the return statement is an essential part of good programming practice, and taking the time to get it right can save you a lot of headaches in the long run.

You can read more about the return statement on MDN.

Profile of Daine

Written by Daine Mawer. Thanks for reading! Im always posting new content. If you liked what you read, please subscribe to my RSS feed or follow me on Github, Twitter or LinkedIn. Im also always on the look out for new oppurtunities, engagements, contract work or just coffee! So please dont hesitate to reach out.