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.

  1. Push
  2. Length
  3. Unshift

The push and shift method can add an element at the end of the array list. let’s see an example of it.

  1. <!DOCTYPE html> 
  2. <html> 
  3. <body> 
  4.  
  5. <h2>JavaScript Arrays</h2> 
  6.  
  7. <p>The push method add a new element to an array.</p> 
  8.  
  9. <button onclick="myFunction()">Try it</button> 
  10.  
  11. <p id="proassignmenthelp"></p> 
  12.  
  13. <script> 
  14. var a = [2,4,5,7,9,4]; 
  15. document.getElementById("proassignmenthelp").innerHTML = a; 
  16.  
  17. function myFunction() { 
  18. a.push(10); 
  19. document.getElementById("proassignmenthelp").innerHTML = a; 
  20. } 
  21. </script> 
  22.  
  23. </body> 
  24. </html> 

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Matlabworks backlink data index 86