How to return a string from a JavaScript function?

JavascriptWeb DevelopmentFront End Technology

To return a string from a JavaScript function, use the return statement in JavaScript.

Example

You need to run the following code to learn how to return a string using return statement −

<html>
   <head>
      <script>
         function concatenate(name, subject) {
            var val;
            val = name + subject;
            return val;
         }
         function DisplayFunction() {
            var result;
            result = concatenate('Amit', ' Java');
            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 12:21:34
Advertisements