Exforsys.com
 
Home Tutorials JavaScript
 

JavaScript Array Object

 

JavaScript Array Object Methods

Page 2 of 2

Methods of Array Object in JavaScript:

There are various methods used with Array object of JavaScript:



  • valueOf()
  • sort()
  • concat()
  • join()
  • pop()
  • push()
  • shift()
  • reverse()
  • slice()
  • splice()
  • toString()
  • unshift()

valueOf():

The method valueOf() of the Array object of JavaScript is used to return the primitive value of an Array object.


NOTE: the valueOf() method of the Array object is usually called automatically by JavaScript and not explicitly by the programmer.


General syntax of the valueOf() method of the Array object of JavaScript:



arrayObject.valueOf()



sort():

The method sort()is used to sort the elements of an array object in JavaScript.


General syntax of the sort() method of the Array object of JavaScript:



arrayObject.sort(sortby)



In the above, the sortby argument is optional. If it is given, it indicates the order in which the sorting must take place which must be defined as a function.


For example:



<html>


   <body>
      <
script type="text/javascript">
         var exforsys = new Array(5)
         exforsys[0] = "Test"
         exforsys[1] = "Welcome"
         exforsys[2] = "Training"
         exforsys[3] = "Good"
         exforsys[4] = "Day"
         document.write("The Original Array is:" +"<br />")
         document.write(exforsys + "<br />")
         document.write("The Sorted Array is:" +"<br />")
         document.write(exforsys.sort())
          
      </script>
   </
body>
</
html>




The output of the above example is:



The Original Array is:
Test,Welcome,Training,Good,Day
The Sorted Array is:
Day,Good,Test,Training,Welcome



The first output statement prints the array as such without sorting. The second document.write statement has sort() method defined in it with the Array Object exforsys. Thus, the array is sorted before printing.




First Page: JavaScript Array Object


Read Next: JavaScript Array Object Methods – Part I



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape