├── index.html
└── script.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Javascript Arrays
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/script.js:
--------------------------------------------------------------------------------
1 | // Crate an array of integrs
2 | const arr = Array.from({ length: 10 }, (_, i) => i + 1); // 1-10
3 | // slice a specific range and store it into new array
4 | const slicedArray = arr.slice(2, 5); // 1-10
5 | // Remove Element From Array By Using splice and store into new array
6 | const removeElement = [];
7 | for (let i = 2; i < 5; i++) {
8 | removeElement.push(arr.splice(2, 1)[0]);
9 | } // 3,4,5 removed arr = 1,2,6,7,8,9,10
10 | // shift the first element of the array and store it into new variable;
11 | const firstElemet = arr.shift(); // 2,3,6,7,8,9,10
12 | // add the element in the beginning of the array;
13 | arr.unshift(0); // 0,2,6,7,8,9,10
14 | // add the element in the end of the array;
15 | arr.push(11); // 0,2,6,7,8,9,10,11
16 | // use pop to remove element from array and store it into variable
17 | const lastElement = arr.pop(); // 0,2,6,7,8,9,10
18 | // move element element from the array to new array using slice method
19 | const movedElement = arr.slice(2, 5); // moved 6,7,8 arr = same as above
20 | // Remove Element From Array By Using splice and store into new array
21 | const removeElement2 = [];
22 | for (let i = 2; i < 5; i++) {
23 | removeElement2.push(arr.splice(2, 1)[0]);
24 | } // removed 6,7,8 arr = 0,2,9,10
25 | // shift elements from the array one by one and store into variables;
26 | const shiftElements = [];
27 | for (let i = 0; i < arr.length; i++) {
28 | shiftElements.push(arr.shift());
29 | } // arr = [9,10]
30 | // const shiftElements = [];
31 | // for (let i = 0; i < 5; i++) {
32 | // shiftElements.push(arr.shift());
33 | // } arr = []
34 |
35 | // add multiple element at the end of the array;
36 | arr.push(12, 13, 14);
--------------------------------------------------------------------------------