1

when I try the following:

if(myFunction() == "3") { alert('its three!'); } 

my function returns undefined, when I know that it is actually returning 3. Is this because javascript evaluates the if statement before the function can return a value?

if so how can I do something like this:

var result = myFunction(); // Wait until result is there if(result == "3") { alert("its three!"); } 
5
  • 3
    What does myFunction do? Can you post the contents of the function? Commented Nov 3, 2010 at 21:14
  • what is in myFunction()? Commented Nov 3, 2010 at 21:14
  • 2
    It works fine. The problem is your function. See this fiddle for proof: jsfiddle.net/t7txg Commented Nov 3, 2010 at 21:15
  • 3
    The "Wait until result is there" comment makes me think there's something asynchronous is going on... who knows... Commented Nov 3, 2010 at 21:20
  • Readin @CMS comment makes me think that you may be returning from an anonymous function defined inside myFunc. This probably doesn't do what you think. Commented Nov 3, 2010 at 21:23

3 Answers 3

2

To troubleshoot your problem, try calling the "alert" function with the return value of your function as the argument:

var result = myFunction(); // For debugging alert(result); if(result == "3") { alert("its three!"); } 

If the contents of the alert box are "undefined", your function is not in fact returning a value, and just assigning that non-existent result to a variable is not going to help. Check to make sure myFunction looks like:

function myFunction() { // Perform calculations var n = 10 - 7; // Return result return n; } 

The JavaScript interpreter requires neither functions to return values nor return statements to be used. That means you should double-check that there is in fact a return statement. If you are assigning the result to a variable first, that can be one way you could miss that return statement.

If you are dealing with Ajax requests, jQuery "modal dialogs," or similar constructs, it is not even possible for a function to return a value in this way (when the button is clicked or the request completes, that is). To fix the problem, your function needs to accept a callback function as a parameter. Then when done, it must call that function, passing the "returned value" as an argument:

function myFunction(callback) { // Start an AJAX request var x = new XMLHttpRequest(); x.open('GET', '/domath.php?expression=10-7', true); x.onreadystatechange = function() { if(x.readyState == 4 && x.status == 200) { // Call callback function, "returning" a value asynchronously callback(x.responseText); } }; x.send(); } 

And then your main code would be:

myFunction(function(result) { // For debugging alert(result); if(result == "3") { alert("its three!"); } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

What it sounds like is that your javascript code is calling the function before the function or the elements it accesses (ie something in the DOM) have been fully loaded, which is why the function call is returning undefined instead of '3'.

The way to prevent this is to defer calling the function until the DOM has finished loading.

This is typically done by having the function call in your document.onload() method, which only gets run when the page has finished loading, or by using jQuery's $.ready() method, which again waits until the page is ready before being run.

Hope that helps.

Comments

1

I am a little unclear as to what you are doing from the description. Try this to see if its what you wanted:

function MyFunction(){ var result // Do something here //dummy: result = 3; return result; } var Testing = MyFunction(); alert(Testing); if (Testing == 3) { alert("Holy Cow! Its 3!"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.