how to add number in array javascript code example

Example 1: adding numbers in an array javascript

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

Example 2: javascript pushing to an array

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

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

Example 3: 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 4: how to insert a value into an array javascript

var list = ["foo", "bar"];


list.push("baz");


["foo", "bar", "baz"] // result

Example 5: how to make and add to an array in javascript

var arrayExample = [53,'Hello World!'];
console.log(arrayExample) //Output =>
[53,'Hello World!']

//You can also do this
arrayExample.push(true);

console.log(arrayExample); //Output =>
[53,'Hello World!',true];

Tags:

Java Example