admin管理员组

文章数量:1291532

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?

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?

Share edited May 20, 2010 at 13:45 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked May 20, 2010 at 13:28 Calibre2010Calibre2010 3,8599 gold badges26 silver badges35 bronze badges 3
  • 1 w3schools./js/js_functions.asp – karim79 Commented May 20, 2010 at 13:30
  • 7 That isn't a jQuery function. – user113716 Commented May 20, 2010 at 13:31
  • 1 Since Javascript isn't strictly typed, while you can easily return a string as shown below, you aren't going to have a strict return type from your method, etc. – justkt Commented May 20, 2010 at 13:43
Add a ment  | 

2 Answers 2

Reset to default 12

Use the return statement:

return "Hello";

https://developer.mozilla/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.

function myFunction(myValue) {
  // do something

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

var result = myFunction(myValue);

本文标签: How do I return a value from a javascript functionStack Overflow