4

I have some Javascript code that needs to end with either a true or false value being returned. However, when the true/false value is computed, the original value has passed through multiple functions, like so:

var txt = 'foo'    
function one(txt) {
if(txt == 'foo') { two(txt); }
}
function two(txt) {
if(txt == 'foo') { three(txt); }
}
function three(txt) {
if(txt == 'foo') { return true; }
else { return false; }
}

Obviously this example has little point but it gets the general point across. What I need to do to it is return the true (or false) value from function three() all the way back to function one(), and then have function one() return that value to whatever called it. I am assuming I have to go back through function two() to get back to one, is there a way I can do this with a variable? Just an idea. Thanks very much for any help!

1
  • 1
    Maybe I don't get the question but just write return two(txt); and return three(txt); won't work? Commented Jul 12, 2010 at 17:04

6 Answers 6

5

You may want to try the following (if I understood your question correctly):

function one(txt) {
   if(txt == 'foo') return two(txt);
   else return false;
}

function two(txt) {
   if(txt == 'foo') return three(txt);
   else return false;
}

function three(txt) {
   if(txt == 'foo') return true;
   else return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

You can coerce to boolean - return two( new Boolean(txt) ); or return two( !!txt ); - without needing the || false.
@Fabio: I removed the || false part. It isn't needed if all functions return either true or false.
2

Change the calls to three() and two() to return three() and return two().

1 Comment

That would still return undefined if you pass something other than foo to the one() function.
0

If you like ternary operators:

function one(txt) {
    return (txt == 'foo') ? two(txt) : false;
}
function two(txt) {
    return (txt == 'foo') ? three(txt) : false;
}
function three(txt) {
    return (txt == 'foo');
}

Comments

0

You can do it like people said above, or you can declare a variable outside the functions so it is global and just refer to it. It is not considered great practice, but it will work.

Comments

0

Try:

var txt = 'foo'    
function one(txt) {
if(txt == 'foo') return two(txt); 
 else return false;
}
function two(txt) {
if(txt == 'foo')  return three(txt); 
 else return false;
}
function three(txt) {
if(txt == 'foo')  return true; 
else return false; 
}

1 Comment

one and two also need else cases, or they won't return anything when the condition is false.
0
var txt = 'foo';

function one(txt) {
   return two(txt); 
}

function two(txt) {
    return three(txt); 
}

function three(txt) {
    return txt == 'foo'
}

3 Comments

A few more words about why you suggest this code would be nice.
I think it's best because it treats the boolean like any other type. It is also the simplest, needing no if's or else's. The boolean is created with the test in function 3, and passed up the stack.
@BarryBurns is correct. This is an example of function composition. one txt === (two of three) txt. This is preferable to the boolean early exit examples because you can avoid using a conditional to return a boolean (this is redundant since a boolean condition evaluates to true or false, and it becomes easy to return a contradiction, which is confusing), and also allows the the creation of complex conditionals by composing more functions together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.