This forum is now read-only. Please use our new forums! Go to forums
I'm stuck on the return keyword and I don't understand one thing about Return
Can someone please explain to a T what return is because i have already looked at other replies and they still confuse me
Answer 50d8dff9f4c9c765510000ac
Hello,
the return
statement allows your function to return
/give a value to the rest of your program.
Some examples:
var squarea = function(x)
{
console.log(x*x);
};
This function would print the square of the passed number into the console.
But if you wanted to calculate cube
with the formula square*length
, you would have to use return, as the function keeps the result.
Well, it prints it to the console, but this doesn’t help you, as you can’t access the console. We have to write a function, which gives you the result, which returns
the result:
var squareb = function(x)
{
return x*x;
};
var cube = function(x)
{
return squareb(x) * x;
};
console.log(cube(10));
This works perfectly.
However, this might seem to be useless, as you could calculate the cube using the formula length*length*length
, but there are many other things, which really need to be done in many functions (especially, as “Seperation of concerns“ is a programming concept, just like D.R.Y.).
Do you have any further questions?
I hope it helped. :-)
Answer 50d912113359ab0223000b21
Return is shorthand for “do the calculation and just give back a number”. The alternative is “do the calculation and save the result as variable x”. Here are two examples, one uses return and other other does not, but they both do exactly the same thing:
Example without return: var area; var cirArea = function(radius) { area = 3.14 * radius * radius; }; cirArea(2); console.log(area);
Example with return: var cirArea = function(radius) { return radius * radius * 3.14; }; console.log(cirArea(2));
Both codes will result in exactly the same thing, which is “12.56” appearing in the console. But, without return you need to use an extra variable (area) to hold the answer. With return, you can shorten your code a little and make it cleaner. Hope that helps.
17 comments
very helpful
Thank you! This helped e understand returns to kinda get a feel of it.
ok, I am also beginer with javascript and I had problems with understanding what “return” means. Based on user “caver” explanation , I understood that “return” acts as unvisible variable. Like this.. return === var name. So “return” is unvisible variable that holds unvisible value
thanx …it was very helpful
Thank you! I couldn’t understand it seemed like return was irrelevant, but the explanation of the extra variable makes more sense
Thank you for providing a nice example and explanation
Okay, so if i am caculating the area of several circles i would just ad more console.log(cirArea(#)) lines and they would automaticaly go through the same calculations? then it would suddenly make sense.
this answer is great! explain it in a easy way! ppl who ask this like me must new to everything, some answer will make us more confused, i like this answer anyway, thx!!!
Thanks for this answer! So basically, it’s just shortening the code
oh thanks so much ! you helped me while i am stucked ! nice answer and example Thanks you
good answer but i still dont understand
well then it is a bad answer..
oh no im still not getting it i feel like killing myself……
i didn’t get it either
This one actually makes sense.
I hope Ethan didn’t kill himself :(
Wow thanks. But I am not sure if my understanding is correct–which is, “return” is used for writing less codes?
Answer 51ef07ac631fe9a98e000290
// Parameter is a number, and we do math with that parameter var timesTwo = function(number) { return number * 2; };
// Call timesTwo here! var newNumber = timesTwo(69); This is the only part that you have to change console.log(newNumber);
I think this lesson was worded very poorly. It feels like this should have been much easier to grasp but I was looking for a more complicated answer.
35 comments
Indeed. Thank you. Seems stupid to overlook it, as I was also looking beyond the immediate solution.
THANK YOU!!!
THanks lol I forgot what ive learned because i was off for 5 days? THanks man
Thx-I was befuddled:-(
Thanks
Cheers! I spent 30 mins looking for a clue.
thanq and u r right this part is not very clear and i think this part should be more clear!!
I replaced line 7, using copy/paste, var newNumber = timesTwo(69) and got this error message: “TypeError: Property ‘log’ of object #
OK, I reloaded the page, and then it worked, following advice I spotted elsewhere.
Why 69?
thank you so much
Yeah, don’t get me wrong. This is a free course and I appreciate it greatly, but some of these are worded so terribly. Hard to understand what I’m supposed to do sometimes.
That was definitely easier than what I thought at first, but it makes sense, you just need to give a value to the function.
Thanks. I was having trouble on this one too. I think I was trying too hard.
hahah i see what you did there .. pffft hehe * whisper * 69 haha .. okay .. ill shut up now ._.
Awesome, thanks a lot!
Thanks this helped alot!
Oh my lord, I made this way harder than it was. Thanks for the help.
Thank you! I was too!
So helpful!
Thanks!!
Thx
Thank you, I thought I had to make newNumber a function, it was worded quite badly!
this really helped thank you
this helped, thanks!
Thank you so much, had no idea wut to do!
Thank you! :)
Hey thanks
Thank you!
thank you
nice work @Wes
Thanks Wes!
So someone tell me if what i am thinking is correct. So the return key takes the answer of a function and injects into the position of the function keyword. I think that is what it does buy im not completely sure.
Thanks! I was looking for a more complicated answer, too.
That’s all I needed !!
Answer 52dffbf9631fe978b80004e6
this is all i did var timesTwo = function(number) { return number * 2; };
// Call timesTwo here! var newNumber = timesTwo(8) console.log(newNumber);
on line 7 var new number = put the timestwo there simple
3 comments
Sweet, thanks!
At last now i understood this “return” stuff to an extend- thanks for your help.
hello, so i tried to just copy and paste your answer as it was the same as mine except my “return number * 2;” was indented and neither work. was your example not supposed to work or am i missing something else?
this is the code that i am putting in var timesTwo = function(number) { return number * 2; };
// Call timesTwo here! var newNumber = timesTwo(8) console.log(newNumber);
so what am i missing?
Answer 53a343a37c82ca93e300075d
- It’s basically asking you to set the variable along with it’s function, and then find a number that will reduce to a number that will have no decimals. As described in the example (which I had to use myself ) it uses 13 % 3 for an example. This translates to the initial return value ( return number / 4;) Which has already divided 13 by 4.
How the code will look with all the modifications.
var quarter = function(number)
{return number / 4;}
if (quarter(***Number goes here!***) %3 == 0 console.log("The statement is true");} else { console.log("Thestatement is false");}
Basically you place a number in between the quarter’s parentheses that can be divided by 4 and then divided about 3 that will leave no decimal.
- I used 48 which divided by 4 ( 48/4 = 12 /3 = 4) no decimals.
2 comments
Oh my God, THANK YOU. Stared on this for about an hour…
THANKSSS’
Answer 520e895180ff339241000f22
Practical use of functions
Have a look at this file and click the Click Me!
button. The source code can be found here.
We use a simple function, which is called, after the button is pressed:
var alertName = function() {
var name = prompt("Please enter your name.");
alert("Nice to meet you, " + name);
};
And we have to tell the browser, what to do. And therefore this function is a list of things, which should be done, when the function is called.
Practical use of return
Have a look at this file and at the source code. Now we splitted the tasks:
var alertName = function() {
var name = getName();
alert("Nice to meet you, " + name);
};
var getName = function() {
return prompt("Please enter your name.");
};
While this seems to be more complicated, return
allows us to seperate the tasks: Now one function asks for the name, while another alert()
s the name. In this small example it is not really needed, but imagine you wanted to add another function reverseString()
, which reverses every string and therefore can be used in many different ways, so reverseString("Homer")
returns "remoH"
and reverseString("Long text")
returns "txet gnoL"
.
Note: I did not implement it in this example, as you’ll later learn, how to do it (simply use a loop and loop from the end of the string to the beginning of the string).
If it helped, I’d also give you further examples using reverseString()
.
9 comments
Made some updates.
Thank you so much!!
You are welcome! :-)
You deserve even more praise for wasting your time, helping young coders like me! ;)
I still don’t get it i think i am the only one :(
I’m sure you aren’t. :-)
nooo, you’re not the only one…
You two arent the only ones too T^T
WTF?
Answer 5388fc6a548c35d471000e57
In simple statement :-
Return is all about “calculate the steps inside a function, then stop the function process, without printing“ and the returned value of the function can be used anywhere within document.
Ex:-
var foo = function(mango) {
return mango * 5;
}
foo(100);
Answer would be 100.
What actually happens is, when you use return, it would execute the return statement, but won’t print right away.
Try to execute the above code without calling the function. What happens? - nothing would be printed! because we haven’t specified to print or show anything, we just used “return” to calculate the step and stop the function, then the function can be called anywhere outside of the function to print as we like.
Try using this code:-
var foo = function(mango) {
console.log(mango * 5);
}
foo(100);
If we use console.log, it prints the answer right away, then what’s the use of using function? so we use return to stop the function for a problem, then call the function-parameter outside from the function to print the solution.
Hope it helped you.
Regards, Mithun.
1 comments
thank you
Answer 541ada52631fe9507200253c
i recommend reading jquery first and you will understand the formatt offhead and heart
var timesTwo = function(number) { return number * 2; };
// Call timesTwo here! var newNumber = timesTwo() console.log(newNumber);
Answer 544c52187c82caac9b0001e1
I got a code if you still get confused: var newNumber = console.log(timesTwo(69)); Well, I just gave you a slight headstart…… maybe. =)
Answer 552ba61d76b8fe9b8e00033d
I also struggled with the return keyword when I did JavaScript. But in PHP they give the following explanation that helped me understand better:
The Return Keyword Instead of printing something to the screen, what if you want to make it the value that the function outputs so it can be used elsewhere in your program? In PHP, the return keyword does just that. It returns to us a value that we can work with. The difference between this and echo or print is that it doesn’t actually display the value.
Think of it like a calculator solving a mathematical problem that takes several steps to complete. The value from each step is computed, but we don’t see the result until we get the final answer. In other words, each value is returned and the final answer is echoed on screen for us.
Hope this helps…
Answer 5501086d51b8875634001cc0
Well, the return basicly means when you’re running a code and you dont want to print alot of stuff out, it’s just like saying, “Wait, I dont want this code to run. I can save myself alot of time.” so I hope this helped.
Answer 53f360cd52f8636ad8000074
Hi i’m having some trouble with the lesson.
i feel like i did everything write in the lesson, but the code keeps using the “else” statement rather than the “if”
can someone take a look and help me out
thanks!!
var quarter = function(number) { return quarter/4; }
if (quarter(48) % 3 === 0 ) { console.log(“The statement is true”); } else { console.log(“The statement is false”); }
1 comments
return number/4
Answer 52cf461f7c82cade5000051e
Hello, the return statement allows your function to return/give a value to the rest of your programm. Some examples: var squarea = function(x) { console.log(x*x); };
This function would print the square of the passed number into the console. But if you wanted to calculate cube with the formula squarelength, you would have to use return, as the function keeps the result. Well, it prints it to the console, but this doesn’t help you, as you can’t access the console. We have to write a function, which gives you the result, which returns the result: var squareb = function(x) { return xx; }; var cube = function(x) { return squareb(x) * x; }; console.log(cube(10));
1 comments
Is there a reason why you copied my answer?
Answer 52bf69eb80ff33697300358f
// Define quarter here. var quarter = function (number) { return(number / 4)
}
if (quarter(48) % 3 === 0) { console.log(“The statement is true”); } else { console.log(“The statement is false”); }
2 comments
thanks..but i had those code but still getig error what will changes if i place 3 to 48 … calculation is right 3 modulo 3 equal 0 “statemant is true”…????
equal: 3,12,48,144 and ie( number multiply 3),
Answer 5215b926548c350747001c3d
6 comments
this IS java
exactly
duh
…
LOL
java is like that var is very important duh
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
33 comments
Thanks for this explanation, it really helped me get my head around the concept!
No problem. ;-)
Still a little confused but it makes a lot more sense now. Thanks!
You are welcome! :-)
gottit! basically a return allows you to use the value you generated from a certain function anytime down the road in your code. (…please correct me if i’m wrong :P )
Correct. :-)
thanks man really helped me :D
Great! :-)
thx!
You’re welcome! :-)
genius
Finally got it! Thanks!
You are welcome! :-)
fucking complicated or my brain tired
that is 3rd longest piece of code I have ever seen
i still dont get it is this too complicated for 13 year olds
Can you be more precise? Which parts cause problems?
im not understanding when you would use it, why, and how. does that help at all
Okay, so the “why would anyone use this” is the main problem. I’ll try to write a new answer below.
thanks
i can’t understand anything about function’s and returns…. that might be for my english thanls anyway
thanks this helped me a lot now my head isn’t as much in the clouds ver helpful and straightforward
You are welcome! :-)
Mr lee the syntax is saying i’m wrong but it should be right! help please?
Would you mind to share your code?
Well this just makes no fucking sense
Would you just avoid saying the f-word?
???????????
this explanation makes me want to swear.
Dude, wtf? Just post the code; thats all we want. :)
uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh, what?
Christopher Chapman - your comment made me laugh…finding it confusing myself - good to hear it’s not just me!
The reason some people dont post the code is because this is a learning academy not find the answer and finish everything with cheating and saying you did it because when you go to do something you wont have any experience at all.