View Full Version : The purpose of Return
WWPRanma
12-19-2003, 07:41 PM
I'm curious as to what the purpose of return : true and return :false is in Javascript. I don't have a good explanation of it in the books I own.
n8thegreat
12-19-2003, 07:48 PM
well sometimes when you do a certain function you want to return values such as
function add(num1, num2) {
var result = num1 + num2;
return result;
}
var sum = add(3,2); //sum is now equal to 5
sometimes when you do functions you simply want it to return true or false to use in conditionals or what have you
function add(num1, num2, sum) {
if( (num1 + num2) == sum ) {
return true;
} else {
return false;
}
}
var addUp = add(2,3,5); //returns true
if( addUp ) {
alert("Yep, it added up");
} else {
alert("Nope, it didn't add up");
}
addUp = add(2,3,7); //returns false
hope that helps explain it
WWPRanma
12-19-2003, 07:53 PM
Well, what I'm more or less trying to figure out is what exactly return true and return false means in plain english. Assume I don't even know anything about programming and try explaining this.
n8thegreat
12-19-2003, 07:55 PM
it means "whatever variable this function was assigned to is now eqaul to whatever is after the 'return' word"
look up above to see
WWPRanma
12-19-2003, 08:01 PM
Ok, so if it returns true, the local variable inside the function is now replaces the global variable of the same name, and if it returns false, the global value will retain it's own value while the local value will hold a value all it's own within the function?
n8thegreat
12-19-2003, 08:11 PM
no it jsut means the global variable is now whatever is after the return word, it doesnt have to be a variable, it could be a math equation. if it is a local variable, its whatever the value of the local variable is. it doesnt have anything to do with its name.
blackbox
12-20-2003, 06:39 PM
Return True means that the coding above will work and the browser will run it unlike if its return false then the browser will return to its original self.
example
<a href="www.google.com" onMouseOver="(window.status='www.google.com');return true" onMouseOut="(window.status='');return false">
that little code wright there will that the status bar say www.google.com when u hover over the link and when the mouse comes off it the browser will return the status to its oringinal self(unless there is other activity in the browser)
Dr. Web
12-20-2003, 08:27 PM
blackbox, that is not an accurate explanation of return.
from www.devguru.com
"The return statement specifies the value to be returned by a function and performs the act of returning that value to where the function was called from. "
n8 did a bangup job of explaining this already, so I won't go into any further detail.
n8thegreat
12-20-2003, 08:37 PM
black box, thats not what return does, its just how the browser handles those dhtml events
Jon Hanlon
12-20-2003, 11:43 PM
All functions in Javascript return a value (the default is undefined). Sometimes you don't want a returned value, so you just never reference it. eg.
function yell() {
alert("WATCH OUT!!!");
}
// Is the same as:
function yell() {
alert("WATCH OUT!!!");
return; // returns undefined
}
yell(); // executes function
var scream = yell(); // pretty useless - scream is now undefined
function addUp(a,b) { // returns a + b
return a + b;
}
var five = addUp(2,3); // uses the returned value
addUp(2,4); // pretty useless by itself
In the case of an event-handler (onclick, onmouseover, onfocus etc.), we can cancel the pending event if we return a false value.
A false value is an empty string (""), the word false (false), or zero (0).
So, if I had a link:
<a href="www.playboy.com" onclick="return false">Hugh H</a>
Then the playboy site will never be visited, as the return false cancels the click action.
It is good practise to return either true or false from all event-handler functions.
WWPRanma
12-23-2003, 01:50 AM
Ok, so what basically is being said here is that "return" reiterates if something is going to happen. Technically you could create a function with no return value on it and it would just perform the function without asking if it needed to be returned true or false because it will just automatically run the function. But if you put a return false on there, it will tell it to hold up and not perform the function because it said return false. And then if you type return A + B, the function is going to add up A + B.
n8thegreat
12-23-2003, 09:18 AM
no no no, the return false only does that with those certain dhtml events.
WWPRanma
12-24-2003, 01:20 AM
Ok, so when is it a good idea to apply return. That's basically what I've been getting at, because I've seen alot of events that pass into functions have return false; applied to them and I was just confused as to when is it or isn't it a good idea to apply return false to it.
n8thegreat
12-24-2003, 09:19 AM
it depends on what you want it to do. if you have this link here:
<a href="http://www.bob.com" onclick="window.open(this.href,'');">asd</a>
then when you click on it, a popup window will popup, and the main window will redirect to bob.com
however if you put a return false like this
<a href="http://www.bob.com" onclick="window.open(this.href,'');return false">asd</a>
then a popup window will popup, but a the main page will NOT redirect. it cancels the other actions that are assigned to it when you do onclick.
only when you want to cancel the other actions associated with the event do you want to use return false. such dhtml events may be onmouseover,onmosueout,onclick,etc...
another example. say i have a link where text is underlined normally, but when you mouseover the underline goes away, if you apply return false to an onmouseout event, then the underline wont come back.
<a href="asdsf" onmouseout="return false">as</a>
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.