Computer >> Computer tutorials >  >> Programming >> Javascript

How does JavaScript 'return' statement work?


A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.

Example

You can try to run the following code to work with return statement in JavaScript −

<html>
   <head>
      <script>
         function concatenate(first, last) {
            var full;
            full = first + last;
            return full;
         }

         function DisplayFunction() {
            var result;
            result = concatenate('John', ' Wright');
            document.write (result );
         }
      </script>
   </head>
   <body>
      <p>Click the following button to call the function</p>

      <form>
         <input type = "button" onclick = "DisplayFunction()" value = "Call Function">
      </form>
   </body>
</html>