JavaScript - of() method

revision:


Category : array

The Array.of() static method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.

Syntax :

        Array.of()
        Array.of(element0)
        Array.of(element0, element1)
        Array.of(element0, element1, /* … ,*/ elementN) 
    

Parameters:

elementN : elements used to create the array.

Examples:

            console.log(Array.of('foo', 2, 'bar', true));
            // Expected output: Array ["foo", 2, "bar", true]
            console.log(Array.of());
            // Expected output: Array []
        

Practical examples

example: using Array.of() method

code:
                    <div>
                        <p id="of-1"></p>
                        <p id="of-2"></p>
                        <p id="of-3"></p>
                        <p id="of-4"></p>
                        <p id="of-5"></p>
                        <p id="of-6"></p>
                    </div>
                    <script>
                        console.log(Array.of(1)); // [1]
                        console.log(Array.of(1, 2, 3)); // [1, 2, 3]
                        console.log(Array.of(undefined)); // [undefined]
                        document.getElementById("of-1").innerHTML = "array.of(): " + Array.of(1);
                        document.getElementById("of-2").innerHTML = "array.of(): " + Array.of(1, 2, 3);
                        document.getElementById("of-3").innerHTML = "array.of(): " + Array.of(undefined);
                        document.getElementById("of-4").innerHTML = "array.of(): " + JSON.stringify(Array.of(1));
                        document.getElementById("of-5").innerHTML = "array.of(): " + JSON.stringify(Array.of(1, 2, 3));
                        document.getElementById("of-6").innerHTML = "array.of(): " + JSON.stringify(Array.of(undefined));
                    </script>
                

example: JavaScript Array.of() method examples

code:
                    <div>
                        <p id="of-7"></p>
                        <p id="of-8"></p>
                        <p id="of-9"></p>
                        <p id="of-10"></p>
                        <p id="of-11"></p>
                    </div>
                    <script>
                        let numbers = Array.of(3);
                        console.log(numbers.length); // 1
                        document.getElementById("of-7").innerHTML = "array length : " + numbers.length;
                        console.log(numbers[0]); // 3
                        document.getElementById("of-8").innerHTML = "numbers : " + numbers[0];
            
                        let chars = Array.of('A', 'B', 'C');
                        console.log(chars.length); // 3
                        document.getElementById("of-9").innerHTML = "characters length : " + chars.length;
                        console.log(chars); // ['A','B','C']
                        document.getElementById("of-10").innerHTML = "characters : " + chars;
                        document.getElementById("of-11").innerHTML = "characters : " + JSON.stringify(chars);
                    </script>