Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
3231
JavaScript Array Object Functions
Introduction
The Array object contains 10 functions. In this section you
will see these 10 functions in some small examples. Please refer to the array
object, if you need more details about it. All functions are used with the
array object. The following examples demonstrate this.
The join function can be used to join the content of an
array to a single string, separated with a character of your choice. If you
don’t specify one, it will use the comma by default.
<html>
<head>
<script type="text/javascript">
var arJoin
= new Array("11", "12", "13", "14", 15)
document.write(arJoin.join("|"))
</script>
</head>
<body></body>
</html>
Output
11|12|13|14|15
JavaScript concat Function
The concat function can be used to join two arrays together
to one array.
<html>
<head>
<script type="text/javascript">
var ar1 =
new Array("11", "12", "13", "14", 15)
var ar2 =
new Array("16", "17", "18", "19", 20)
document.write(ar1.concat(ar2))
</script>
</head>
<body></body>
</html>
Output
11,12,13,14,15,16,17,18,19,20
JavaScript pop Function
The pop function can be used to retrieve the last element of
an array.
<html>
<head>
<script type="text/javascript">
var ar1 =
new Array("11", "12", "13", "14", 15)
document.write(ar1.pop())
</script>
</head>
<body></body>
</html>
Output
15
JavaScript push Function
The push function can be used to add one ore more new
elements to an array.
<html>
<head>
<script type="text/javascript">
var ar1 =
new Array("11", "12", "13", "14", 15)
ar1.push("16","17")
for(var i =
0;i<ar1.length;i++)
document.write(ar1[i]+"
| ")
</script>
</head>
<body></body>
</html>
Output
11 | 12 | 13 | 14 | 15 | 16 | 17 |
JavaScript reverse Function
The reverse function can be used to reverse to order of the
elements in an array. The first will become the last and the last will become
the first.
Before Reverse: 11,12,13,14,15
After Reverse: 15,14,13,12,11
JavaScript shift Function
The shift function can be used to remove the first element
of an array. It also returns the first element.
<html>
<head>
<script type="text/javascript">
var arShift
= new Array("11", "12", "13", "14", 15)
document.write(arShift.shift())
</script>
</head>
<body></body>
</html>
Output
11
JavaScript slice Function
The slice method can be used to create a new array from a
specific range in an array. You have to define the starting position. You can
also define the end position, however if you do not specify it then it will get
the array position from your starting point to the end point of the array. The
following example demonstrates it.
<html>
<head>
<script type="text/javascript">
var arSlice
= new Array("11", "12", "13", "14", 15)
document.write(arSlice.slice(2,4)
+ "<br>")
// Without
defining end
document.write(arSlice.slice(2))
</script>
</head>
<body></body>
</html>
Output
13,14
13,14,15
JavaScript splice Function
The splice function can be used to add or remove elements of
an array. You need to specify the starting position and how many elements to
remove. The following example shows how to remove and add elements with that
function.
<html>
<head>
<script type="text/javascript">
var
arSplice = new Array("11", "12", "13",
"14", 15)
The sort function can be used to sort the elements in an
array. You can also create a separate compare function to use with sort.
However if you do not, then it will convert all elements to strings and sort
them in dictionary order. This can cause problems when you have integers in an
array and need to sort them – i.e.: 11 would come before 3.