style.css

 body {            font-family: Arial, sans-serif;            margin: 20px;            padding: 0;        }        #container {            max-width: 500px;            margin: 0 auto;            padding: 20px;            border: 1px solid #ddd;            border-radius: 5px;        }        h2 {            text-align: center;        }        input[type=\\\"text\\\"] {            width: calc(100% - 24px);            padding: 10px;            margin-bottom: 10px;            border: 1px solid #ccc;            border-radius: 3px;        }        button {            width: 100%;            padding: 10px;            background-color: #28a745;            color: #fff;            border: none;            border-radius: 3px;            cursor: pointer;        }        button:hover {            background-color: #218838;        }        h4 {            margin-top: 20px;            color: #555;        }

\\\"Image

","image":"http://www.luping.net","datePublished":"2024-07-31T16:24:03+08:00","dateModified":"2024-07-31T16:24:03+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > push() Method in JavaScript

push() Method in JavaScript

Published on 2024-07-31
Browse:614

The push() method in JavaScript adds one or more elements to the end of an array. This method modifies the original array and returns the new length of the array.

Syntax :

array.push(element1, element2, ..., elementN);

*Example 1.: *

const fruits = ["Apple", "Banana"];
fruits.push("Orange", "Mango");
console.log(fruits); // Output: ["Apple", "Banana", "Orange", "Mango"]

*Example 2.: *
How to Dynamically Add Elements Using the push() Method

index.html



    
    
    Fruit List
   


    

Fruit List

style.css

 body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 0;
        }
        #container {
            max-width: 500px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        input[type="text"] {
            width: calc(100% - 24px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        button:hover {
            background-color: #218838;
        }
        h4 {
            margin-top: 20px;
            color: #555;
        }

Image description

Release Statement This article is reproduced at: https://dev.to/sudhanshu_developer/explanation-of-push-method-in-javascript-3cb5?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3