How to return a string from a JavaScript function?


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>

Updated on: 19-Jun-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements