JavaScript Learning Notes: Arrays


                    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 ]

  • pop: Removes the last element from the array.

  • 
                        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

  • includes: Checks if a certain value exists in the array.
  • indexOf: Returns the first index at which a given element can be found in the array, or -1 if it is not present.

  • 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 ]


  • slice: Returns a shallow copy of a portion of an array into a new array. The original array is not modified.
  • 
                        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.


  • splice: Changes the contents of an array by removing or replacing existing elements and/or adding new elements.
  • 
                        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.


  • When you use push() to add an array to another array, the entire array is added as a single element (nested array).
  • 
                        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


  • You can merge two or more arrays using the concat() method or the spread operator [...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' ]


  • Spread Operator: Another way to merge arrays, more concise and flexible.
  • 
                        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' ]


  • The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
  • 
                        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 ]


  • Array.isArray: Checks whether a variable is an array.
  • 
                        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


  • Array.from: Creates a new array from an array-like or iterable object.
  • 
                        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' ]
    []


  • The Array.of() method creates a new Array instance with a variable number of elements.
  • 
                        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.