Multiple ways to return multiple values from a function in javascript with examples
Javascript function returns a single value variable by definition. It contains only single return statements and types.
How do functions return multiple values? You can include multiple values in either arrays or objects in javascript.
There are multiple ways to return multiple values from a function in javascript and Ecmascript2015
Javascript function returns multiple values with native javascript
The following function returns a single value by default.
function function() {
return variable
}
Function in java script returns multiple values using either Array or an object.
- use an object
The object contains multiple keys and values, So if you want to send multiple values, Wrap multiple values in an object. values of the object are wrapped in {}
Following is an example of writing a javascript native function
function getValues() {
return { number1: 10, number2: 20 };
}
console.log(getValues());
- use an Array
The array contains multiple keys and values, So if you want to send multiple values, Wrap multiple values in an Array. values of the array are wrapped in []
Following is an example of writing a javascript native function
function getValues() {
return [10, 20];
}
console.log(getValues());
Output:
[10,20]
The above javascript functions can be rewritten with the following things in ES6
- replace functions declaration with function lambda expression
- Assign array and object values to variables using destructing assignment
## ES6 function return multiple values
For example, the javascript function returns an array as given below.
Array return from a function assigned to variables using array destructing assigning. It is assigned to individual variables.
```markdown
function getEmployee()
{
var id=11;
var name="john";
var salary=4000;
return [id,name,salary]
}
console.log(getEmployee())
let [id,name,salary] = getEmployee()
console.log(id)
console.log(name);
console.log(salary)
```
Output:
[11, "john", 4000]
11
john
4000
The same function can be rewritten lambda expressions, also called functional expressions. if the expression body contains a single statement, a return is not required.
const getEmployee=()=>{
var id=11;
var name="john";
var salary=4000;
return [id,name,salary]
}
console.log(getEmployee())
let [id,name,salary] = getEmployee();
console.log(id)
console.log(name);
console.log(salary)
The below example contains function returns object
that has three values - id, name, and salary.
function getEmployee()
{
var id=11;
var name="john";
var salary=4000;
return {id:id,name:name,salary:salary}
}
console.log(getEmployee())
let {id:id,name:name,salary:salary} = getEmployee()
console.log(id)
console.log(name);
console.log(salary)
Output:
{id: 11, name: "john", salary: 4000}
11
john
4000
Rewrite the above function as a functional lambda expression that returns an object And also rewritten {id:id,name:name,salary:salary}
to {id,name,salary}
that is equaval to {id: 11, name: "john", salary: 4000}
const getEmployee = () => {
var id = 11;
var name = "john";
var salary = 4000;
return { id, name, salary };
};
console.log(getEmployee());
let { id: id, name: name, salary: salary } = getEmployee();
console.log(id);
console.log(name);
console.log(salary);
Conclusion
The function always returns a single value by definition. If you want to send multiple values, Include them in Array or Object. You can also assign returned values from a function assigned to variables using object and array destructing assignment.