Insert in Array JavaScript:- Geniusofstudent

push

For adding new items at the end of an array we can use push() method. 
Syntax 
Array.push(item1,item2....itemN);

unshift
For adding new items at the beginning of an array we can use unshift() method. 
Syntax 
Array.unshift(item1,item2....itemN); 

Change value by array index
Array values can access using array index value, we can replace the array value by using its index value. 
Syntax 
array_variable[index]=value;





Code:-
  • <!DOCTYPE html> 
    1. <html> 
    2.  
    3. <head> 
    4.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    5. </head> 
    6.  
    7. <body> 
    8.  <h1>Actual Array Value : [10,20]</h1> 
    9.  
    10.  <input type="button" onclick="ar_fun();" value="Insert to Array"> 
    11.  
    12.  <script> 
    13.   function ar_fun() 
    14.   { 
    15.  
    16.    var ar = [10, 20]; 
    17.  
    18.    alert("First : " + ar); 
    19.  
    20.    //Method 1 
    21.    ar.push(30); //Insert the value into the last position 
    22.  
    23.    alert("push(30) Inserted Last : " + ar); 
    24.  
    25.    //Method 2 
    26.    ar.unshift(5); //Insert the value into the first position 
    27.    alert("unshift(5) Inserted First : " + ar); 
    28.  
    29.    //Method 3 we can replace value into selected index 
    30.    ar[1] = 100; 
    31.    alert("ar[1] = 100 Replace to 1st index : " + ar); 
    32.  
    33.   } 
    34.  </script> 
    35.  
    36. </body> 
    37.  
    38. </html>

    Insert in Array JavaScript:- Geniusofstudent Insert in Array  JavaScript:- Geniusofstudent Reviewed by Network security on July 11, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.