BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
JavaScript
JavaScript Arrays
JavaScript Variables
How to use JavaScript?
JavaScript Guidelines
Stock sorter example for IE5
Confused about how to use a data island in IE5??
Software Release tracking application
Employee Phonebook dataisland
Multitab DHTML browser
Trim function in Javascript
Multiple Grid
Re: Trim Function in JavaScript
Re: Trim Function in JavaScript
Trim Function in JavaScript using Regular Expressions.
DHTML ScoreBoard Behavior
Re: JScript Super Class To Handle XML Transformations
Updated: Detect XML Parser version using Javascript
XML Cache using IE 5.0 Behaviour
XML cache on client for the desktop application website
Javascript XML element listing
 
LAMP >>

By :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.

JavaScript join Function
JavaScript concat Function
JavaScript pop Function
JavaScript push Function
JavaScript reverse Function
JavaScript shift Function
JavaScript slice Function
JavaScript splice Function
JavaScript sort Function
JavaScript unshift Function

JavaScript join Function

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.

<html>

<head>

<script type="text/javascript">

            var arRev = new Array("11", "12", "13", "14", 15)

            document.write("Before Reverse: " + arRev + "<br/>")

            document.write("After Reverse: "+arRev.reverse())

</script>

</head>

<body></body>

</html>

Output

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)

            document.write(arSplice.splice(2,4, "99") + "<br>")

            // adding new elements

            document.write(arSplice)

</script>

</head>

<body></body>

</html>

Output

13,14,15
11,12,99

JavaScript sort Function

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.

<html>

<head>

<script type="text/javascript">

            var arSort = new Array("11", "10", "7", "8", "9")

            document.write("Normal sorting: " + arSort.sort() + "<br>")

           

            function SortNumeric(x,y)

            {

                        return x-y

                                   

            }

            document.write("Sorting with function: " + arSort.sort(SortNumeric))

</script>

</head>

<body></body>

</html>

Output

Normal sorting: 10,11,7,8,9
Sorting with function: 7,8,9,10,11

JavaScript unshift Function

The unshift function can be used to add new elements at the beginning of the array. This function only works on Netscape.

<html>

<head>

<script type="text/javascript">

            var arUnshift = new Array("11", "12", "13", "14", "15")

            document.write(arUnshift.unshift("8","9","10"))

</script>

</head>

<body></body>

</html>

Output

8


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
conference calls
swimming pool builder
cfxmasks
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP