What Does Return Mean in JavaScript?

In JavaScript, the return keyword is used to return values from functions. It can also be used to exit a function early. In this article, we will discuss what the return keyword does in JavaScript, and provide some examples of how it can be used.

JavaScript return Keyword

The return keyword in JavaScript is used to return values from functions. It can also be used to exit a function early. In the following example, we have a function that calculates the sum of two numbers. The return keyword is used to return the value of the sum back to the caller:

function add(a, b) {
  return a + b;
}

console.log(add(12, 13)); // Output: 25

As you can see, when we call the add() function, it returns the value of 25. We can also use the return keyword to exit a function early:

function add(a, b) {
  if (a === 0 || b === 0) {
    return;
  }
  return a + b;
}

console.log(add(12, 13)); // Output: 25
console.log(add(0, 13)); // Output: undefined

In the above example, we have added a condition to our add() function that checks if either of the values is 0. If so, the function returns immediately without executing the rest of the code. Otherwise, it will continue to execute and return the sum of the two numbers. We can also return objects from functions:

function createUser(name) {
  return { name: name };
}

console.log(createUser("John")); // Output: { name: 'John' }

In the createUser() function, we are returning an object with a single property, name. The return value can also be a string, number or boolean:

function calculateTax(price) {
  if (price > 100) {
    return "Tax is applied to all prices over $100";
} else {
    return "Tax is not applied to any prices"
}
}

console.log(calculateTax(120)); // Output: Tax is applied to all prices over $100
console.log(calculateTax(-40)); // Output: Tax is not applied to any prices

The calculateTax() function takes a price as input and returns either a string or boolean value. In the first example, we are returning a string that says “Tax is applied to all prices over $100”. In the second example, we are returning a boolean value of false, which indicates that tax is not applied to prices under $100.

You can also return multiple values from a function using an array or object literal:

function getUserInfo() {
  return [ "John", 34 ];
}

console.log(getUserInfo()); // Output: [ 'John', 34 ]

In the getUserInfo() function, we are returning an array with two values: “John” and 34. We could also have returned an object literal with two properties: name and age:

function getUserInfo() {
  return { name: "John", age: 34 };
}

console.log(getUserInfo()); // Output: { name: 'John', age: 34 }

As you can see, the return keyword is a powerful tool that can be used in many different ways. In the next section, we will discuss how the return keyword can be used to exit a function early.

Returning Early from Functions

As we saw in the last section, the return keyword can be used to return values from functions. It can also be used to exit a function early. In the following example, we have a function that checks if a user is logged in. If they are not logged in, the function prints a message and exits:

function checkUser() {
  if (!loggedIn) {
    console.log("You are not logged in");
   }
}
checkUser(); // Output: You are not logged in

The checkUser() function checks if the user is logged in. If they are not, the function prints a message and exits. We can also use the return keyword to exit a loop:

while (count <= 20) {
		 count++;
	}
return; // Exit the while loop early

In the above example, we have a while loop that counts from 0 to 20. The return keyword is used to exit the loop early. This can be useful if you want to exit a loop based on a condition.

Conclusion

In this article, we have answered the question: what does return mean in javascript? We have seen that the return keyword can be used to return values from functions, and also to exit functions and loops early. We hope that you have found this article helpful. Thank you for reading!

Leave a Comment