0

I'm trying to pass a variable from one function to another. I thought this would work but I get undefined.

function launch(){
    amount();
    finalize(theAmount);
}

function amount(){
    var theAmount = prompt('How much?');
    return theAmount;
}

function finalize(theAmount){
     alert(theAmount);   
}

launch();
1
  • "theAmount" is lost on the stack bro. You could just declare it outside the functions (not best practice but works though). Commented Dec 21, 2013 at 5:54

2 Answers 2

4

You are trying to access the variable which is defined in some other function. Thats NOT possible because of Javascript's scope restrictions. You have to pass the return value as it is or you have to assign it to a variable and then pass it to the function.

Either this

function launch(){
    finalize(amount());
}

Or

function launch(){
    var theAmount = amount();
    finalize(theAmount);
}
Sign up to request clarification or add additional context in comments.

Comments

0

you are calling amount function in lunch function which is returns a value but you haven't receive it.

try this to modify lunch function as

function launch(){
    var theAmount = amount();
    finalize(theAmount);
}

function amount(){
    var theAmount = prompt('How much?');
    return theAmount;
}

function finalize(theAmount){
     alert(theAmount);   
}

launch();

Comments

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.