views:

153

answers:

2

Say I have a function

function myFunction(myValue) {

    // do something

}

How would I return a value from this, say a string type after the method is executed?

+10  A: 

Use the return statement, just like you would with any other JavaScript function:

return "Hello";

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/return


Also, JavaScript is dynamically typed so you don't have to specify a type for the function, just return the variable.

Andy E
jQuery equivalent: `return $("Hello".split("")).map(function(){ return this }).get().join("");`
Roatin Marth
@Roatin Marth: Thanks, that's really useful ;-)
Andy E
Not enough jQuery :P
Pablo Cabrera
+7  A: 
function myFunction(myValue) {
  // do something

  // return to caller of function:
  return myValue;
}

var result = myFunction(myValue);
Alec