Is it required to have a return a value from a JavaScript function?


A JavaScript function can have an optional return statement i.e. it’s optional to return a value. This statement should be the last statement in a function.

For example, you can pass two numbers in a function and then you can expect the function to return their multiplication to your calling program.

Example

Try the following example. It defines a function that takes two parameters and concatenates them before returning the resultant in the calling program.

Live Demo

<html>
   <head>
      <script type = "text/javascript">
         function concatenate(first, last){
         var full;

         full = first + last;
            return full;
         }
         function secondFunction(){
            var result;
            result = concatenate('John', 'Doe');
            document.write (result );
         }
      </script>
   </head>

   <body>
      <p>Click the following button to call the function</p>
      <form>
         <input type = "button" onclick = "secondFunction()" value = "Call Function">
      </form>
      <p>Use different parameters inside the function and then try...</p>
   </body>

</html>

Updated on: 08-Jan-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements