JavaScript Tutorial/Function/Function Return

Материал из Web эксперт
Перейти к: навигация, поиск


The return keyword will exit the existing function and return a value.

   <source lang="javascript">


   <script language="JavaScript">
   <form name="form1">
   Value: <input type="text" Name="tmp" Size=5>

<input type="button" name="get" value="Get Returned Value" onClick="fill()">

</form> </body> </html></source>

Return an array from a function

   <source lang="javascript">


  Iteration Two
  function makeArray() {
       var myArray = new Array(4);
       myArray[0] = "A";
       myArray[1] = "B";
       myArray[2] = "C";
       myArray[3] = "D";
       return myArray;
  function showArray(theArray){
       var quote = "";
       for (var i = 0; i < theArray.length; i++){
           quote += theArray[i] + " ";
       return quote;    

<SCRIPT> var x = makeArray(); document.write(showArray(x)); </SCRIPT>



Return boolean value from function

   <source lang="javascript">

<html> <head> <script Language="Javascript" type = "text/javascript">

</script> <title>Capturing Links</title> </head>

<A href="" onClick = "return CheckRequest("wbex")">Java</A>

<A href="" onClick = "return CheckRequest("Netscape")">Netscape</A>

</body> </html></source>

Returning Values

JavaScript provides a return statement to return a value.

The value to be returned is placed after the keyword return.

The "undefined" value is returned from a function if no value is specified after the keyword return or if the return statement is not used.

A value returned from a function can be assigned to a variable or used within an expression.

   <source lang="javascript">

<html> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </html></source>

Return value from a function

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body onLoad="calcAvg();"> </body> </html></source>