push
Syntax
Array.push(item1,item2....itemN);
unshift
Syntax
Array.unshift(item1,item2....itemN);
Change value by array index
Syntax
array_variable[index]=value;
Code:-
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head>
- <body>
- <h1>Actual Array Value : [10,20]</h1>
- <input type="button" onclick="ar_fun();" value="Insert to Array">
- <script>
- function ar_fun()
- {
- var ar = [10, 20];
- alert("First : " + ar);
- //Method 1
- ar.push(30); //Insert the value into the last position
- alert("push(30) Inserted Last : " + ar);
- //Method 2
- ar.unshift(5); //Insert the value into the first position
- alert("unshift(5) Inserted First : " + ar);
- //Method 3 we can replace value into selected index
- ar[1] = 100;
- alert("ar[1] = 100 Replace to 1st index : " + ar);
- }
- </script>
- </body>
- </html>
Insert in Array JavaScript:- Geniusofstudent
Reviewed by Network security
on
July 11, 2019
Rating:
No comments: