Arrays

How does one add the values of an array

Depends where in the array you want to add them.

You want to add them to the end use the push() method.

let arr = [ 1, 3, 3 ]
arr.push(7);
console.log(arr) // prints  [ 1, 3, 3, 7 ]

Or you can use unshift() to add items to the start of an array.

let arr = [ 3, 3, 7 ]
arr.unshift(1);
console.log(arr) // prints  [ 1, 3, 3, 7 ]

You can replace values by reassigning them

let arr = [ 1, 3, 3, 8 ]
arr[3] = 7;
console.log(arr) // prints [ 1, 3, 3, 7 ]

Hope this helps,

Happy coding!

1 Like