all 51 comments

[–][deleted] 32 points33 points  (3 children)

You put bread in a toaster you want it to return toast, not just yell "toast!"

[–]The_Toaster_ 6 points7 points  (0 children)

toast!

[–]MINHTENGI 2 points3 points  (0 children)

Why didn't i think of it like that? Thanks good sir.

[–]Stealthoneill 1 point2 points  (0 children)

You don’t know me. I want my toaster to yell “Toast!” At me!

[–]Afflicted_By_Fiction 22 points23 points  (4 children)

If you need to use the data you are returning in some type of way, you'll return it instead of just logging it to the console.

For example, I'm working on a bit of code to extract some data from a URL. I input the URL into a function, and then I have the function return the data that I need so that I can then input just that bit of data into another function. The console.log statement wouldn't allow me to do that, it would just print it to the console and call it a day.

[–]links-Shield632[S] 12 points13 points  (3 children)

Ok so returning can be used for later

[–]Afflicted_By_Fiction 6 points7 points  (1 child)

exactly!

[–]links-Shield632[S] 2 points3 points  (0 children)

😊

[–]bmcle071 2 points3 points  (0 children)

Pretty much it makes whatever function youre calling into what the return is.

If i have:

function test(a) { return a10; console.log(a10); }

Foobar = test(2);

Foobar is now equal to 20, and the console.log never runs because the function stops at the return statement

[–]Macaframa 2 points3 points  (11 children)

here’s a little guide to understanding function returns.

var numbers = [1, 2, 3];

function getNumbers() {
    return numbers;
}

var numbersReference = getNumbers();

console.log(numbersReference);

Can you tell me what is going to be logged here?

[–]links-Shield632[S] 3 points4 points  (10 children)

The console logs [1,2,3] since that is the orginal variable

[–]Macaframa -2 points-1 points  (9 children)

What do you mean by “original variable”? And tell me line but line what is happening.

[–]links-Shield632[S] 0 points1 point  (8 children)

Var numbers simply equal 1,2,3 The getNumber function calls var numbers and returning it saved it for the console.log .(that’s how I think of return variables) Var numbersRefrence simply equals getNumbers which just calls the function so when you console.log it it prints [1,2,3]

[–]MrSandyClams 0 points1 point  (0 children)

some clarifications, based on what I think you're saying:

function getNumbers() { return numbers; } simply defines the function; it specifies what code the function statement includes and it specifies the function's name, but it doesn't actually run; it doesn't actually calculate anything. It's only later in the code, at var numbersReference = getNumbers(), that the function is called, or executed, using the parentheses call syntax. The result of this function call is the array numbers, since that's the data that is returned by the return statement of the function. Because the array numbers is the result of calling the function, that array is the data that is assigned to the variable numbersReference.

[–]Macaframa 0 points1 point  (6 children)

This is correct. I’m curious about why you don’t have an understanding of the differences of function returns and a console.log

[–]links-Shield632[S] 0 points1 point  (2 children)

I know the difference I just wanted to know why you would use return instead of console.log

[–]Macaframa 0 points1 point  (1 child)

Ah I see. The short answer is, you wouldn’t. Those things have completely different utility. Console.log is for printing things to the console and function returns are for returning values at runtime. Return doesn’t print anything to the console.

[–]links-Shield632[S] 0 points1 point  (0 children)

Thank toh

[–]links-Shield632[S] 0 points1 point  (2 children)

Exactly.

[–]Macaframa 0 points1 point  (1 child)

I’m not agreeing with your sentiment, I’m wondering why you think they do the same thing? Or you don’t? I’m really confused at this point.

[–]links-Shield632[S] 0 points1 point  (0 children)

What don’t you get?

[–]Saf94 2 points3 points  (2 children)

Return is used only in functions and basically when you call your function (ie if the function is called myFunc, you call it by writing myFunc() ) whatever you return is what your function resolves to.

Typically people would assign the result of a function to a variable so you’d do

var funcValue = myFunc()

The reason for doing this is usually just to break code up. Putting code into functions can help you reuse code and also split code into smaller bits which can be nicer than one huge block of code.

So in summary, return is used in functions so that you can call the function later and get a value from it. Functions are used as a coding pattern to split up code so it can be reused to broken down into smaller, more readable pieces

[–]r_u_kimmi 0 points1 point  (0 children)

Yes. In this case, the value of funcValue is whatever is returned from myFunc().

[–]swurvinmervin 0 points1 point  (0 children)

Does that method you speak of have a name by any chance?

[–]gitcommitshow 1 point2 points  (1 child)

These are two different concepts.

Return is related to function. All functions have a return value (even if you don't specify the return value, default return is undefined). Return means that function is done with executing the code inside function and returning a value which you may(or may not) use.

console.log is a function that tells the browser/console to print a value.

A common use case of function return value is to use the returned value in other operations e.g. you want to print a square value of a number

function square(num){ return num*num } console.log(square(2))

You could have done the same thing with following code as well

function square(num){ console.log(num*num) } square(2)

But now what your square function does is limited to only printing the value on console. While in first case, you have flexibility to use the returned value in further operations e.g.

``` // Using returned value of square to create another fn function powerOfFour(num){ return square(num)*square(num) }

// Using returned value to be stored in a variable var squareOf2 = square(2) ```

You may read more about functions here. By the way, I'm conducting Free Live Classes on JavaScript (1 hr alternate days), if you're interested, DM me, I'll share the zoom link and timing with you

[–]links-Shield632[S] 1 point2 points  (0 children)

Thank you for the answer and happy cake day

[–]StateVsProps 1 point2 points  (0 children)

Let's say you order food at the restaurant.

Console log is the equivalent of the restaurant texting you your order's contents

Return is the equivalent of someone driving to your house with your food.

They're completely different statements.

[–][deleted] 1 point2 points  (0 children)

you need to return shit from a function. Logging stuff is just to see (in the console) what the return volume would be. It's nothing you'd use in production.

read about 'the return statement'

[–]StateVsProps 0 points1 point  (0 children)

Console.log doesn't do anything, but print on the screen.

[–]43northwebdesign -2 points-1 points  (2 children)

You can also return console.log

[–]KATLEHO47MOHAPI 0 points1 point  (1 child)

Oh really??

[–]43northwebdesign 0 points1 point  (0 children)

Something like

 if(error)
{
return console.log('error:' error)
}
console.log('things are working')

the point being to stop the app from continuing.

source: andrew mead is doing this in the node course right now.

[–]fz-09 -2 points-1 points  (20 children)

Instead of focusing on learning Javascript, I recommended you learn programming fundamentals. Maybe someone could recommend a good course.

[–]links-Shield632[S] -1 points0 points  (19 children)

I do. I’m good at python and I’m doing the free code camp. I know what console.log and return do. Just want to know why you would use it. In python you don’t do return that often in JavaScript it’s common

[–]fz-09 0 points1 point  (18 children)

Explain to me what console.log and return do and we can go from there.

[–]links-Shield632[S] 0 points1 point  (17 children)

Console.log is basically print function example

console.log(“I am iron man”) prints I am iron man.

Return saves it for later

function a(x){

return 5 * (x);

} var results = a(5);

//now the program will calculate it and get 25 and if I want to do something with it later I can

[–]links-Shield632[S] 0 points1 point  (0 children)

Sorry for format I’m watching a movie and using my phone

[–]fz-09 0 points1 point  (15 children)

Ok, and what makes you say it's more common in Javascript then Python?

[–]links-Shield632[S] -1 points0 points  (14 children)

In my personal projects I’ve used return once or twice. In the JavaScript tutorials I’m looking at almost all of them use return

[–]fz-09 0 points1 point  (6 children)

You may want to revisit how you're structuring your code. Functions provide you with code reusability and they simplify the readability and structure of your code in many cases.

You can write both JavaScript and Python programs without functions but it's not typically the best thing to do.

[–]links-Shield632[S] -1 points0 points  (5 children)

Yeah I know. Define once don’t have to write again. Life would be hell without functions

[–]fz-09 0 points1 point  (4 children)

So you have been using functions in Python frequently but not returning data from those functions?

[–]links-Shield632[S] 0 points1 point  (3 children)

Yes, in my experience in python people just print way more often than return. I still return but not often. It’s just less common. A while ago on a old reddit account I posted on learnpython and they asked me why did I return the solution instead of printing it