Array.of()

Array.of() convert values ​​to arrays. Just like we saw with creating new arrays using the Array constructor or function, Array.of() behaves in a very similar fashion.

        var arr = Array.of("true", "codes");
        var control = Array.isArray(arr);
        console.log(control) // true

        

While Array.of(2) creates a new array with a single element, 2 , and a length property of 1, Array(2) creates a new empty array with 2 empty slots and a length property of 2.

        var arr1 = Array.of(2);
        var arr2 = Array(2);
        console.log(Array.isArray(arr1)); // true
        console.log(Array.isArray(arr2)); // true
        console.log(arr1); // [2]
        console.log(arr2); // [,] , so length=2

2 thoughts on “Array.of()

Add yours

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started