What method can you use to add an element to an array in JavaScript?
In Javascript
If you want to add an element to an array , there are 3 to 4 method which can be used to add an element to an array.
- Push
- Length
- Unshift
The push and shift method can add an element at the end of the array list. let’s see an example of it.
- <!DOCTYPE html>
- <html>
- <body>
- <h2>JavaScript Arrays</h2>
- <p>The push method add a new element to an array.</p>
- <button onclick="myFunction()">Try it</button>
- <p id="proassignmenthelp"></p>
- <script>
- var a = [2,4,5,7,9,4];
- document.getElementById("proassignmenthelp").innerHTML = a;
- function myFunction() {
- a.push(10);
- document.getElementById("proassignmenthelp").innerHTML = a;
- }
- </script>
- </body>
- </html>
Comments
Post a Comment