Dynamic array :- JavaScript



  • Array is used to store multiple value in a single variable.
  • By using for loop we can add the values dynamically into the array.
  • The push() method is used to add new values into the array.
  • Code:-
    1. <!DOCTYPE html> 
    2. <html> 
    3.  
    4. <head> 
    5.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    6.  
    7.  <script> 
    8.   var _arr = []; 
    9.  
    10.   function create_array() 
    11.   { 
    12.    _arr = []; 
    13.    for (i = 0; i < 5; i++) 
    14.    { 
    15.     _arr.push(i); 
    16.    } 
    17.    alert("Array Created"); 
    18.   } 
    19.  
    20.   function view_array() 
    21.   { 
    22.    alert(_arr); 
    23.   } 
    24.  </script> 
    25. </head> 
    26.  
    27. <body> 
    28.  
    29.  <input value="Create Array" type="button" onclick="create_array();"> 
    30.  <input value="View Array" type="button" onclick="view_array();"> 
    31.  
    32. </body> 
    33.  
    34. </html>


  • Dynamic array :- JavaScript Dynamic array :- JavaScript Reviewed by Network security on November 13, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.