Return Multiple Values from a Function in JavaScript

Web Dev
Sponsored links

In this article, I will introduce 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>
Sponsored links

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>