C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Function: The importantValues function returns an array of 3 values. These could be computed variables or constants.
functionJavaScript program that returns multiple values
function importantValues() {
"use strict";
// Return 3 values in an array.
return [1, 2, "cat"];
}
var result = importantValues();
console.log("RETURN VALUE 1: " + result[0]);
console.log("RETURN VALUE 2: " + result[1]);
console.log("RETURN VALUE 2: " + result[2]);
Output
RETURN VALUE 1: 1
RETURN VALUE 2: 2
RETURN VALUE 2: cat
Tip: In the V8 engine a special optimization is used to accelerate property lookups, so this approach is blazingly fast.
JavaScript program that sets properties on object
function petValues() {
"use strict";
// Return multiple values in an object.
return {animal: "cat",
size: 100,
color: "orange",
name: "fluffy"};
}
var pet = petValues();
console.log("RETURN VALUE: " + pet.animal);
console.log("RETURN VALUE: " + pet.size);
Output
RETURN VALUE: cat
RETURN VALUE: 100