how to add array elements code example
Example 1: javascript append to array
var colors=["red","white"];
colors.push("blue");
Example 2: add array to array javascript
const list1 = ["pepe", "luis", "rua"];
const list2 = ["rojo", "verde", "azul"];
const newList = [...list1, ...list2];
Example 3: javascript adding an array to an array
let chocholates = ['Mars', 'BarOne', 'Tex'];
let chips = ['Doritos', 'Lays', 'Simba'];
let sweets = ['JellyTots'];
let snacks = [];
snacks.concat(chocholates);
snacks.concat(chocolates, chips, sweets);
Example 4: js add to array
const myArray = ['hello', 'world'];
myArray.push('foo');
myArray.unshift('bar');
myArray.splice(2, 0, 'there');
Example 5: sum of arrays
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, n, sum=0;
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter %d elements in the array: ", n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of all elements of array = %d", sum);
return 0;
}
Example 6: how to make and add to an array in javascript
var arrayExample = [53,'Hello World!'];
console.log(arrayExample)
[53,'Hello World!']
arrayExample.push(true);
console.log(arrayExample);
[53,'Hello World!',true];