Javascript declares the method of a function and calls the return value of the function

  • 2020-03-30 03:36:35
  • OfStack

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
  <title></title> 
  <!--js Methods that declare functions in --> 
  <script type="text/javascript"> 
    //Because javascript is a weakly typed language, you don't need to type arguments. Functions don't have to have the same requirements as c#, so all paths need to have a return value (this is not like the c# language, and c# methods don't need the function keyword before the method name).
 
    function add(i, j) { //Now it's just declaring a function here, and it's only executed when it's called.
      return i + j; 
 
    } 
    alert(add(5, 6)); //The output of 11
 
    //Not all paths in js have a return value, if there is no return value, he thinks the return value is undefined
    function sum(x, y) { 
      if (x > y) { 
        alert(x + y); 
      }      
    } 
    var z = sum(2, 6); //Because 2 is not greater than 6 the sum function returns no value. If there is no return value he thinks the return value is undefined.
    alert(z); //So it outputs undefined
         
  </script> 
</head> 
<body> 
 
</body> 
</html>

Related articles: