how to return multiple values from a function javascript


var values = getValues();
var first = values[0];
var second = values[1];




function getNames() {
// get names from the database or API
let firstName = 'John',
lastName = 'Doe';

// return values
return {
firstName,
lastName
};
}

//function that returns multiple values
function getTopTwoColors() {
return ["blue", "pink"];
}
var topTwoColors=getTopTwoColors();
var firstValue=topTwoColors[0]; //get first return value
var secondValue=topTwoColors[1]; //get second return value

www.codegrepper.com
www.stackoverflow.com

how to push values in array


some_array = ["John", "Sally"];
some_array.push("Mike");

console.log(some_array); //output will be =>
["John", "Sally", "Mike"]

var vegetables = ['Capsicum',' Carrot','Cucumber','Onion'];
vegetables.push('Okra');
//expected output ['Capsicum',' Carrot','Cucumber','Onion','Okra'];
// .push adds a thing at the last of an array

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.push("Kiwi");

array.push(element)

const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

www.codegrepper.com
www.stackoverflow.com