Page 86 - JavaScript
P. 86

Chapter 6: Arrays




        Syntax



            •  array = [value, value, ...]
            •  array = new Array(value, value, ...)
            •  array = Array.of(value, value, ...)
            •  array = Array.from(arrayLike)


        Remarks



        Summary: Arrays in JavaScript are, quite simply, modified Object instances with an advanced
        prototype, capable of performing a variety of list-related tasks. They were added in ECMAScript
        1st Edition, and other prototype methods arrived in ECMAScript 5.1 Edition.

        Warning: If a numeric parameter called n is specified in the new Array() constructor, then it will
        declare an array with n amount of elements, not declare an array with 1 element with the value of
        n!


         console.log(new Array(53)); // This array has 53 'undefined' elements!


        That being said, you should always use [] when declaring an array:


         console.log([53]); // Much better!



        Examples


        Standard array initialization



        There are many ways to create arrays. The most common are to use array literals, or the Array
        constructor:


         var arr = [1, 2, 3, 4];
         var arr2 = new Array(1, 2, 3, 4);


        If the Array constructor is used with no arguments, an empty array is created.


         var arr3 = new Array();


        results in:


         []


        Note that if it's used with exactly one argument and that argument is a number, an array of that



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