|
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:
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:
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.
Trackback(0)

|