add numbers to an array javascript code example

Example 1: adding numbers in an array javascript

console.log(
  [].reduce((a, b) => a + b)
)

Example 2: how to push values in array

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"]

Example 3: array.push

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

Example 4: add numbers from array nodejs

//these are the values//
var values = [
	'1',
  	'2'
]
//makes a variable named total, adding together the values
var total = values[0] + values[1]

//prints out the variable named total
console.log(total);