Page 237 - Web_Application_v2.0_C12_Fb
P. 237

For example:

                    typeof fruits[20]
                 Output: undefined

                 For example:
                 Consider the following code:

                    const cars = ["Honda", "BMW", "Audi", "Porsche"];
                 Write command in JavaScript to:
                   Add an item “Volvo “ to the array cars in the last.

                   Remove first element from the array.
                   Display number of elements in the array.
                   Add following array to an array “cars”.

                    const person=["Rajan", "Yagya", "Munish"];
                 Example 45: To perform array operations

                    <!DOCTYPE HTML>
                    <HTML>
                    <HEAD>
                    <TITLE>Array Operations in JavaScript</TITLE>
                    <SCRIPT>
                    const cars = ["Honda", "BMW", "Audi", "Porsche"]; // Define the array
                    cars.push("Volvo");
                    // 2. Remove the first element
                    cars.shift();
                    // 3. Get the number of elements
                    var count = cars.length;
                    // 4. Add another array "person" to the "cars" array
                    const person = ["Rajan", "Yagya", "Munish"];
                    cars = cars.concat(person); // Merge the arrays
                    // Display the results using document.write
                    document.write("<h2>Array Operations in JavaScript</h2>");
                    document.write("<b>Updated Cars Array:</b> [" + cars.join(", ") + "]<br>");
                    document.write("<b>Number of Elements:</b> " + count);
                    </SCRIPT>
                    </HEAD>
                    </HTML>
                 Output:
















                                                                                                  JavaScript Part 2  235
   232   233   234   235   236   237   238   239   240   241   242