//Function that returns multiple values - Using destructuring
function foobar(foo, bar) {
return [foo, bar];
}
const [one, two] = foobar(1, 2);
function twoValues() {
return [0, 1];
}
let [first, second] = twoValues();
// With new JavaScript syntax, you can do this easily!
function returnsMultiple(){
return ["foo", "bar"];
}
let [firstValue, secondValue] = returnsMultiple();
function getNames() {
// get names from the database or API
let firstName = 'John',
lastName = 'Doe';
// return as an array
return [firstName, lastName];
}
Code language: JavaScript (javascript)