What does return mean?

Tell us what’s happening:
Describe your issue in detail here.
I had posted a question about what arr.push vs arr.shift meant but now i’m just wondering what “return” means? I’ve jumped into the true or false exercises and I noticed a significant number of lessons that ask for returns and that’s what’s confusing me.

  **Your code so far**

function welcomeToBooleans() {

// Only change code below this line

return true; // Change this line

// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15

Challenge: Understanding Boolean Values

Link to the challenge:

So a good place to start is with an understanding of functions, from a mathematical standpoint.

Math has functions, like those on a calculator. sin(x) is a calculator function, as is x^2 (can’t figure how to do squared on my phone, but that’s the x-squared function). When we type a number on a calculator and press a function, the displayed number changes, becoming that number run through that function.

That new number? That’s the “returned value” of that function. Press 9.2, then the squared operation, the returned value (or return) from that function is 84.64.

The same principle applies to functions in the programming sense - we take some data, feed it into a function, the function uses that data to provide us with something else, and returns that something else to us.

If we have a reverseString function, and we feed it a string, it will likely give us back (or return) that string in reverse order. Wherever that function is evaluated (or called), that’s where it’ll return that value.

If we don’t need it for some reason, we don’t need to receive that returned value… But every function returns a value, even if it’s returning undefined.

2 Likes

Hi Sherif123,

snomonkey’s explanation is absolutely right.

Maybe you can also see it like a deal that you make with your function.

Let’s take this function as an example:

function foo(a, b) {
    return a + b
}

Part of the deal is that you give your function some arguments and receive something back, a result.

Now if you translate that to our example function it would be like so:

You give function “foo” two parameters “a” and “b” and therefore you receive a result from “foo”, that’s the sum of “a” + “b”.

“foo” must first compute the result of “a” + “b” and then use the “return” keyword to hand that value over to you.

Now you probably ask yourself: does a function always has to return something?

And the answer is no.

Here is an example that takes an argument but does not return a thing:

funtion greedy(a) {
    console.log("I am greedy ", a);
}

best,
Dennis

1 Like

Not entirely accurate, as functions always return something. If we don’t specify what to return, the default return value of undefined will be sent back. There is a very valid reason for this behavior, but it’s part of a much deeper conversation.

Otherwise, good explanation.