How to return a value from a JavaScript function?

JavascriptWeb DevelopmentFront End Technology

To return a value from a JavaScript function, use the return statement in JavaScript. You need to run the following code to learn how to return a value −

Example

<html>
   <head>
      <script>
         function concatenate(name, age) {
            var val;
            val = name + age;
            return val;
         }
         function DisplayFunction() {
            var result;
            result = concatenate('John ', 20) ;
            document.write (result );
         }
      </script>
   </head>
   <body>
      <p>Click the following button to call the function</p>

      <form>
         <input type = "button" onclick = "DisplayFunction()" value = "Result">
      </form>
   </body>
</html>
raja
Published on 16-Feb-2018 16:49:53
Advertisements