Page 87 - JavaScript
P. 87

length with all undefined values will be created instead:


         var arr4 = new Array(4);


        results in:


         [undefined, undefined, undefined, undefined]


        That does not apply if the single argument is non-numeric:


         var arr5 = new Array("foo");


        results in:


         ["foo"]


        6


        Similar to an array literal, Array.of can be used to create a new Array instance given a number of
        arguments:


         Array.of(21, "Hello", "World");


        results in:


         [21, "Hello", "World"]


        In contrast to the Array constructor, creating an array with a single number such as Array.of(23)
        will create a new array [23], rather than an Array with length 23.

        The other way to create and initialize an array would be Array.from


         var newArray = Array.from({ length: 5 }, (_, index) => Math.pow(index, 4));


        will result:


         [0, 1, 16, 81, 256]


        Array spread / rest


        Spread operator


        6


        With ES6, you can use spreads to separate individual elements into a comma-separated syntax:


         let arr = [1, 2, 3, ...[4, 5, 6]];  // [1, 2, 3, 4, 5, 6]



        https://riptutorial.com/                                                                               44
   82   83   84   85   86   87   88   89   90   91   92