How to return multiple values from a function in JavaScript
A function cannot return multiple values. However, you can return similar results by returning an array containing multiple values, as shown in the following example.
Example 1: How to return multiple values from a function in JavaScript
// Function definition
function Sum(a, b, c){
var s = a + b + c;
var arr = [a, b, c, s];
return arr;
}
// Store the returned value in a variable
var arr = Sum(1, 2, 3);
// Display values
console.log(arr[0]); // Output: 1
console.log(arr[1]); // Output: 2
console.log(arr[2]); // Output: 3
console.log(arr[3]); // Output: 6
Example 2: How to return multiple values from a function in JavaScript
You can also return an object, as shown in the following example:
// Function definition
function Sum(a, b, c){
var s = a + b + c;
var obj = {
a: a,
b: b,
c: c,
s: s
};
return obj;
}
// Store the returned value in a variable
var obj = Sum(1, 2, 3);
// Display values
console.log(obj.a); // Output: 1
console.log(obj.b); // Output: 2
console.log(obj.c); // Output: 3
console.log(obj.s); // Output: 6




