const myArr = [0, 1, 2, 3, 4, 5]
myArr.push(6)
myArr.push(7)
console.log(myArr)
Console Output:
[
0, 1, 2, 3,
4, 5, 6, 7
]
This will add 6 and 7 to the end of myArr
.
const myArr = [0, 1, 2, 3, 4, 5]
myArr.pop()
console.log(myArr)
Console Output:
[ 0, 1, 2, 3, 4 ]
const myArr = [0, 1, 2, 3, 4, 5]
myArr.unshift(9)
console.log(myArr)
Console Output:
[
9, 0, 1, 2,
3, 4, 5
]
This will add 9 to the beginning of myArr
.
const myArr = [0, 1, 2, 3, 4, 5]
myArr.shift()
console.log(myArr)
Console Output:
[ 1, 2, 3, 4, 5 ]
This will remove the first element from myArr
.
const myArr = [0, 1, 2, 3, 4, 5]
console.log(myArr.includes(9));
console.log(myArr.indexOf(3));
Console Output:
false
3
Joining Elements
You can use the join()
method to combine all elements of an array into a single string, with a specified separator.
const newArr = myArr.join();
console.log(newArr);
Array Slicing and Splicing
slice and splice are two important methods for manipulating arrays:
const myArr = [0, 1, 2, 3, 4, 5]
console.log("A ", myArr);
Console Output:
A [ 0, 1, 2, 3, 4, 5 ]
const myArr = [0, 1, 2, 3, 4, 5]
const myn1 = myArr.slice(1, 3)
console.log(myn1);
Console Output:
[ 1, 2 ]
slice(1, 3)
extracts elements from index 1 up to, but not including, index 3.
const myArr = [0, 1, 2, 3, 4, 5]
const myn2 = myArr.splice(1, 3)
console.log("C ", myArr);
console.log(myn2);
Console Output:
C [ 0, 4, 5 ]
[ 1, 2, 3 ]
splice(1, 3)
removes 3 elements starting from index 1.
const marvel_heros = ["thor", "Ironman", "spiderman"]
const dc_heros = ["superman", "flash", "batman"]
marvel_heros.push(dc_heros)
console.log(marvel_heros);
console.log(marvel_heros[3][1]);
Console Output:
[ 'thor', 'Ironman', 'spiderman', [ 'superman', 'flash', 'batman' ] ]
flash
[...array]
.
const marvel_heros = ["thor", "Ironman", "spiderman"]
const dc_heros = ["superman", "flash", "batman"]
const allHeros = marvel_heros.concat(dc_heros)
console.log(allHeros);
Console Output:
[ 'thor', 'Ironman', 'spiderman', 'superman', 'flash', 'batman' ]
const marvel_heros = ["thor", "Ironman", "spiderman"]
const dc_heros = ["superman", "flash", "batman"]
const all_new_heros = [...marvel_heros, ...dc_heros]
console.log(all_new_heros);
Console Output:
[ 'thor', 'Ironman', 'spiderman', 'superman', 'flash', 'batman' ]
const another_array = [1, 2, 3, [4, 5, 6], 7, [6, 7, [4, 5]]]
const real_another_array = another_array.flat(Infinity)
console.log(real_another_array);
Console Output:
[
1, 2, 3, 4, 5,
6, 7, 6, 7, 4,
5
]
const marvel_heros = ["thor", "Ironman", "spiderman"]
console.log(Array.isArray("Hitesh")) // Outputs: false
console.log(Array.isArray(marvel_heros)); // Outputs: true
Console Output:
false
true
console.log(Array.from("Hitesh"))
console.log(Array.from({ name: "Hitesh" })); // Outputs: [] (object is not array-like)
Console Output:
[ 'H', 'i', 't', 'e', 's', 'h' ]
[]
let score1 = 100
let score2 = 200
let score3 = 300
console.log(Array.of(score1, score2, score3));
Console Output:
[ 100, 200, 300 ]
This method is useful for creating arrays with different elements without mixing them up.