What is return in JavaScript
Understanding the Concept of 'Return' in JavaScript
When you're first learning to code, JavaScript can seem full of complex terms that can be hard to understand. One of these terms is 'return'. While it might seem intimidating, 'return' is actually quite a simple concept.
The Basic Function of 'Return'
In JavaScript, 'return' is used in a function to stop the execution of that function and send a value from that function back to where it was called. To put it simply, 'return' is like the messenger in a game of telephone. It takes the result from the function, and it delivers it back to wherever the function was called from.
For example, let's say we have a function that adds two numbers together.
function addNumbers(num1, num2) {
let sum = num1 + num2;
return sum;
}
In this case, the 'return' statement is taking the result of num1 + num2
and sending it back to wherever the function addNumbers
was called.
How 'Return' Works in Practice
Let's delve a little deeper into how 'return' works. When a function is called, the browser will jump to that function and run it. When it hits a 'return' statement, it will immediately stop running the function and jump back to where it came from.
For example, let's look at another simple function:
function sayHello(name) {
return "Hello, " + name;
}
console.log(sayHello("Alice"));
When the browser hits console.log(sayHello("Alice"))
, it sees that it needs to run the sayHello
function. It jumps to that function, runs it with name
equal to "Alice"
, and then hits the 'return' statement. It then takes "Hello, Alice"
and jumps back to the console.log
statement, replacing sayHello("Alice")
with "Hello, Alice"
. The console then logs "Hello, Alice"
as you would expect.
The Importance of 'Return'
You might be wondering why 'return' is necessary. Can't we just use console.log
instead? The answer is that while console.log
is useful for debugging and seeing what your code is doing, 'return' is actually how functions give results.
If you were to replace 'return' with console.log
in the previous function, console.log(sayHello("Alice"))
would log the correct greeting, but it would also return undefined
because console.log
doesn't return anything.
function sayHello(name) {
console.log("Hello, " + name);
}
console.log(sayHello("Alice")); // logs "Hello, Alice" and then "undefined"
On the other hand, when we use 'return' in our function, console.log(sayHello("Alice"))
correctly logs "Hello, Alice"
, showing that our function is returning the expected result.
Conclusion
The 'return' keyword in JavaScript is like the courier of the programming world. It takes the result of a function and brings it back to the location where the function was called. Without 'return', we wouldn't be able to use functions to their full potential, and we'd have to rely on less reliable means of getting our results. Just like you wouldn't want to rely solely on word-of-mouth to get information, you wouldn't want to rely solely on console.log
to get your results from functions. Remember, 'return' is your trusty steed, ready to deliver your results exactly where you need them.