Unshift
function is used to add an element to the front of the array.
Syntax
arryNam.unshift(element);
array.html
<!DOCTYPE html> <html> <head> <title>Array</title> </head> <body> <script type="text/javascript"> var array = []; function printArray(arr) { document.write("<br />Elements in Array are : "); for (var i = 0; i < arr.length; i++) { document.write(arr[i] + " "); } document.write("<br />"); } printArray(array); document.write("<br />Appending element"); array.unshift(2); printArray(array); document.write("<br />Appending element"); array.unshift(3); printArray(array); document.write("<br />Appending element"); array.unshift(5); printArray(array); document.write("<br />Appending element"); array.unshift(7); printArray(array); </script> </body> </html>
Open above
page in browser, you can able to see following data.
Elements
in Array are :
Appending
element
Elements
in Array are : 2
Appending
element
Elements
in Array are : 3 2
Appending
element
Elements
in Array are : 5 3 2
Appending
element
Elements
in Array are : 7 5 3 2
No comments:
Post a Comment