Return Multiple Values from a Function in JavaScript

02/14/2022

Contents

In this article, you will learn how to return multiple values from a function in JavaScript.

What is the return value?

The return value is the value returned after processing in the function.

To use the return value, use return.

return [expression]; 

If the expression is omitted, undefined is returned.

Below is sample code that returns one value.

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <title>Return one value</title>
  </head>
  <body>
    <script>
      function greet(name) {
        return 'Hello, ' + name;
      }
      console.log(greet('Michael'));
    </script>
  </body>
</html>

Return Multiple Values

Below is sample code that returns multiple values.

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <title>Return one value</title>
  </head>
  <body>
    <script>
      function greet(name) {
        return ['Hello, ' + name, 'Goodbye, ' + name];
      }
      console.log(greet('Michael'));
    </script>
  </body>
</html>