You are currently viewing 3 Simple Ways to Return Multiple Values in JavaScript Function

3 Simple Ways to Return Multiple Values in JavaScript Function

JavaScript Function Return Multiple Values

We can returned one value in javascipt function. But we can use array, object and also map to return multiple values ​​in a function. Let’s see the best ways to return multiple values in javascript function.

Returning multiple values in JavaScript functions can make your code more efficient, organized, flexible, and compatible with other libraries and frameworks.

3 ways to return multiple values in function

1. Using object:

We can create object with properties for each value you want to return. Then we return that object from the function easily. Here is the example,

function getData() {
  return {
    name : 'John',
    age  : 27,
    email : 'john@gmail.com'
  };
}
let { name, age, email } = getData();

console.log(name, age, email);
  1. In this example code, we have created the function ‘getdata()’.
  2. Inside the function, we directly returned object with properties ‘name’, ‘age’ and ’email’ and each properties have respective values.We can also assign the property value inside the function separately like this:
    function getData() {
      let name = 'John',
      age = 27,
      email = 'john@gmail.com'
    
      return {
        'name' : name,
        'age'  : age,
        'email' : email
      };
    }
  3. Then we destructuring assignment to assign the values of those properties to the variables name, age, and email. The destructuring works like this:
    let names = getData();
    
    let name = names.name,
          age = names.age,
          email = names.email;

    So, the destructuring is used to easy code and reduce the codeline.

  4. Finally, we logged three properties and we got the output.
    John, 27, john@gmail.com

2. Using array

We store multiple elements in an array, in which we can return a specific value to the function using the array index.

function getValues() {
  return ['John', 27, 'john@gmail.com'];
}

const [name, age, email] = getValues();
console.log(name, age, email);

 // Output:John, 27, john@gmail.com
  1. In this above example, we have created the function getValues() and returned comma-separated array values ​in square brackets.
  2. Then we destructuring assignment to assign the values in the returned array to the variables name, age,and email. We can pass same number of arguments as we returned arrayvalues.
  3. Finally, we logged the destrcturing variables name, age, and email.

3. Return Arguments:

We can use the keyword ‘arguements’ to return function arguments. We can simply return the arguments object from the function. Then we use destructuring assignment or other techniques to extract the values from that object.

function getValues() {
  return arguments;
}

const [name, age, email] = getValues('John', 27, 'john@gmail.com');
console.log(name, age, email);
  1. We returned the ‘arguments’ object in the ‘getValues()’ function. It returns the arguments we pass when we call the function.
  2. Then we destructuring assignment to assign the first three argument ( ‘John’, 27, ‘john@gmail.com’) and we passed to the function to the variables name, age, and email.

Leave a Reply