In Javascript you can add one or more element into an array with the hepl of push() method and it will return new length of an array .
Syntax ->
arrayname.push(element1, element2 ,.........elementN )
Parameters ->
elementN- > New Element/s will be added to the end of the array .
Return->It will return new lenght of an array after adding the element into an array .
Example ->
1) Adding New elements into an array ->
var names = [ 'Mukesh ' , ' Ravi ' , 'Abhishek ' , ' Ayush ' ] ; <br>
var arrnames = names.push( 'Ishan ' , ' Medha ') ; <br>
document.write( names ) ; //output -> Mukesh Ravi Abhishek Ayush Ishan Medha <br>
document.write( arrnames ) ; //output -> It will return the length of new array "6".
2) Merging of two arrays
var fname = [ 'Mukesh ' , ' Ravi ' , 'Abhishek ' , ' Ayush ' ] ; <br>
var lname = [ ' Tomar ' , ' Badoni ' , ' Kumar ' , ' Gupta ' ] ; <br>
Array.prototype.push.apply(fname , lname) ;<br>
console.log(fname) // output -> 'Mukesh ' , ' Ravi ' , 'Abhishek ' , ' Ayush ' ' Tomar ' , ' Badoni ' , ' Kumar ' , ' Gupta '
0 Comment(s)