Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»How to Append Element to Array in JavaScript

    How to Append Element to Array in JavaScript

    By RahulMarch 17, 20171 Min Read

    The push() method is used to add elements in JavaScript array. This tutorial will help you to initialize an array in JavaScript. After that append some more elements in Array using push() method in JavaScript.

    Advertisement

    Append Element to Array in JavaScript

    First, create an array in JavaScript. For example, we are creating an array games with two elements “chess” and “football”.

    1
    var games = ['chess', 'football'];

    Now append two more elements “tennis” an “pool” in games array using push() method. This method returns the number of element in array after adding new elements.

    1
    var total = games.push('tennis', 'pool');

    Finally you can print the elements of array using console.log() function. You can also check the number of elements by printing total variable value.

    1
    2
    console.log(games); // ["chess", "football", "tennis", "pool"]
    console.log(total); // 4

    • How to Remove Array Element by Value in JavaScript

    Working Example:

    Below is the working example of initializing an Array in JavaScript. After that append some more elements in existing array. At the end print the array element to console.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <script type="text/javascript">
     
      var games = ['chess', 'football'];
      console.log("---- Array element initial ----");
      console.log(games);
     
      var total = games.push('teniss', 'pool');
      console.log("---- Array element after append ----");
      console.log(games);
      console.log("Total count = " + total);
     
    </script>

    Added above content in a html file and check access page in the browser. Now check the console of the browser, You will find the results as below screenshot.

    array javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to list all collections in MongoDB database

    What is difference between var, let and const in JavaScript?

    What is difference between var, let and const in JavaScript?

    How do I Replace String in JavaScript

    How to Replace String in JavaScript

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • 20 Basic Linux Commands for the Beginners (Recommended)
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.