const numbers = [3, 2, 1]
const newFirstNumber = 4
// => [ 4, 3, 2, 1 ]
[newFirstNumber].concat(numbers)
// => [ 3, 2, 1, 4 ]
numbers.concat(newFirstNumber)
if you want to avoid mutating your original array, you can use concat.
const numbers = [3, 2, 1]
const newFirstNumber = 4
// => [ 4, 3, 2, 1 ]
[newFirstNumber].concat(numbers)
// => [ 3, 2, 1, 4 ]
numbers.concat(newFirstNumber)
if you want to avoid mutating your original array, you can use concat.
let cats = ['Bob'];
// => ['Willy', 'Bob']
cats.unshift('Willy');
// => ['Puff', 'George', 'Willy', 'Bob']
cats.unshift('Puff', 'George');
Add items to the beginning and returns the new array length.
let cats = ['Bob', 'Willy', 'Mini'];
cats.shift(); // ['Willy', 'Mini']
Remove an item from the beginning and returns the removed item.
const fruits = ["apple", "orange", "banana"];
const fruit = fruits.pop(); // 'banana'
console.log(fruits); // ["apple", "orange"]
Remove an item from the end and returns the removed item.
// Adding a single element:
const cart = ['apple', 'orange'];
cart.push('pear');
// Adding multiple elements:
const numbers = [1, 2];
numbers.push(3, 4, 5);
Add items to the end and returns the new array length.
add | remove | start | end | |
---|---|---|---|---|
push |
â | â | ||
pop |
â | â | ||
unshift |
â | â | ||
shift |
â | â |
// Accessing an array element
const myArray = [100, 200, 300];
console.log(myArray[0]); // 100
console.log(myArray[1]); // 200
const numbers = [1, 2, 3, 4];
numbers.length // 4
const fruits = ["apple", "orange", "banana"];
// Different data types
const data = [1, 'chicken', false];