how to return multiple elements in javascript code example
Example 1: javascript return multiple values from a function
function getTopTwoColors() {
return ["blue", "pink"];
}
var topTwoColors=getTopTwoColors();
var firstValue=topTwoColors[0];
var secondValue=topTwoColors[1];
Example 2: return multiple values in javascript
function getNames() {
let firstName = 'John',
lastName = 'Doe';
return {
firstName,
lastName
};
}
Example 3: javascript function multiple return
function doubleEachElement(num1, num2) {
return [num1 * 2, num2 * 2];
}
const [a, b] = doubleEachElement(68, 100);