Question:
How to return a javascript function?
?
2014-04-15 09:40:01 UTC
I know how to make a function (I hope). My own problem is i don't fully understand how to return it?












This is what i got so far, but it don't go though? What am i doing wrong?
Also what are other way i can return a function?
Three answers:
?
2014-04-15 09:56:25 UTC
"Returning a function" doesn't really make much sense (although it's possible in theory). Usually a function will return a value.



I don't even get what you're trying to do there; but if you want to use a variable's value, don't put it in quotes.



Here's some example code:



var myfunction = function(parameter) {

// alert the supplied parameter

alert("You called this function and passed: " + parameter);

// return half

return parameter / 2;

};



var returnedValue = myfunction(50);

document.write("The function returned: " + returnedValue);
Arian
2014-04-15 10:53:02 UTC
1) Add a *return* statement to the body of myfunction on the last line:

function myfunction(lastName) {

return lastName;

}



2) Call the function anywhere in your program.

document.write( myfunction("Flakes") );



Good luck!
?
2014-04-15 09:52:41 UTC
you are printing string value in document.write not variable value

you should wight like -



var lastname=flakes.value;

document.write(lastname);



for returning value in javascript you can use return statement like



return lastname;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...