Shrikant Baheti Java Script Programs All Possible Way To Create Array In Javascript

All Possible Way To Create Array In Javascript

 <script>

        //using Constant Keyword

        const arr1 = [“one”, “two”, “three”];

        document.write(“This is constant array :- ” + arr1 + “<br> <br>”);

        //using Let Keyword

        let arr2 = [“1”, “2”, “3”];

        document.write(“This is array using let :- ” + arr2 + “<br> <br>”);

        //using Var Keyword

        var arr3 = [“1”, “2”, “3”];

        document.write(“This is array using var  :- ” + arr3 + “<br><br>”);

        //line Breaking Array

        let arr4 = [

            “one “,

            ” two “,

            ” three”

        ]

        document.write(“Thi is line breaking arrayn :- ” + arr4 + “<br><br>”)

        //You Can Also Wrte An Array By Providing Index Number;

        var arr5 = [];

        arr5[0] = “One”;

        arr5[1] = “Two”;

        arr5[2] = “Three”;

        arr5[3] = “Four”;

        ;

        document.write(” Array By Providing Index Number :- ” + arr5 + “<br><br>”)

        // Declare Array Using New Keyword

        const arr6 = new Array(“one”, “Two”, “Three”);

        document.write(“Elements Are ” + arr6 + “<br><br>”)

        // Array Elements Can Be Object

        var arr8 = { one: “1”, two: “2”, three: “3” }

        document.write(“Elements Are ” + arr8.one + “<br><br>”)

    </script>

O/P:-

This is constant array :- one,two,three

This is array using let :- 1,2,3

This is array using var :- 1,2,3

Thi is line breaking arrayn :- one , two , three

Array By Providing Index Number :- One,Two,Three,Four

Elements Are one,Two,Three

Elements Are 1

Related Post