Skip to content

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 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: