|
| 1 | +// Array |
| 2 | + |
| 3 | +const myArr = [0,2,3,4,5,6] |
| 4 | + |
| 5 | +console.log(myArr[0]); // Zeroth based indexing output => 0 |
| 6 | + |
| 7 | +const myHeros= ['ankit','kumar','singh'] |
| 8 | + |
| 9 | +const myArr2 = new Array(1,2,3,4,5) // Another Way of intialise an array |
| 10 | +console.log(myArr2) |
| 11 | + |
| 12 | +console.log(myArr[2]) // output => 3 |
| 13 | + |
| 14 | + |
| 15 | +// Array Methods |
| 16 | + |
| 17 | +myArr.push(6) // push element in array after last index of array |
| 18 | +console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6] |
| 19 | + |
| 20 | +myArr.push(7) |
| 21 | +console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6, 7] |
| 22 | + |
| 23 | +myArr.pop() // pop the last index element of an array |
| 24 | +console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6] |
| 25 | + |
| 26 | +myArr.unshift(9); // right shift of every element of an array and push element at starting of index |
| 27 | +console.log(myArr) // output => [9,0, 2, 3, 4,5, 6, 6] |
| 28 | + |
| 29 | +myArr.shift(9); // left shift of every element of an array and pop element at starting of index |
| 30 | +console.log(myArr) // output => [0, 2, 3, 4,5, 6, 6] |
| 31 | + |
| 32 | +console.log(myArr.includes(9)) //output => false |
| 33 | +console.log(myArr.indexOf(3)) //output => 2 if not present return -1 |
| 34 | + |
| 35 | +const newArr = myArr.join() //Adds all the elements of an array into a string, separated by the specified separator string |
| 36 | + |
| 37 | +console.log(myArr); // output => [0, 2, 3, 4,5, 6, 6] |
| 38 | +console.log(newArr); // output => 0, 2, 3, 4,5, 6, 6 |
| 39 | + |
| 40 | +//slice ,splice |
| 41 | + |
| 42 | +console.log("A",myArr); // output => A [0, 2, 3, 4,5, 6, 6] |
| 43 | + |
| 44 | +const myn1 = myArr.slice(1,3) // Returns a copy of a section of an array |
| 45 | +console.log(myn1) // output => [ 2, 3 ] |
| 46 | + |
| 47 | +console.log("B",myArr); // output => B [0, 2, 3, 4,5, 6, 6] |
| 48 | + |
| 49 | +const myn2 = myArr.splice(1,3) // Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. |
| 50 | +console.log(myn2); // output => [ 2, 3, 4 ] |
| 51 | + |
| 52 | +console.log("C",myArr); // output => C [ 0, 5, 6, 6 ] |
| 53 | + |
| 54 | + |
| 55 | + |
0 commit comments