save up to 40%

The Array object, Its Properties, and Methods.

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

The Array object, Its Properties, and Methods.

Rating:

In most programming languages, variables are used to store values in memory. A variable can store only one value at a time. In order to store ten values, ten separate variables are created with different names. However, the problem increases with the growth in the number of variables. In order to solve this problem, arrays are used. An array is a data structure provided for storing and manipulating an ordered collection of data. In JavaScript, the Array object is used to store more than one value. The Array object is a global object that stores data at different indexes for further use. When an array is declared, its size is defined in the parentheses. The given size is the number of elements that can be stored by an array variable. Each array element can be accessed by its position in the array. These positions are called indexes. The index of the first element is zero and the index of the last element is one less than the total number of elements present in an array.

The syntaxes for creating an array are as follows:

var arrayvar=new Array(size)

var arrayvar=new Array(value1, value2...)


Some examples of creating an array are as follows:

var arr=new Array(5) // The variable arr can store five values.

var arr=new Array(1,2,3,4,5) // The variable arr contains five elements.

The following example demonstrates how to insert the values in the array elements:

var arr=new Array(5)
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5


The following example demonstrates how to retrieve the values from an array element:

document.write(arr[0])

The following example demonstrates how to retrieve the data from all the elements in an array:

document.write(arr)

The following table displays the properties and methods of the Array object:

PropertiesMethods
lengthconcat()
prototypejoin()
pop()
push()
reverse()
shift()
slice()
sort()
unshift()

The length property: The length property of the Array object is used to extract the total number of elements present in an array.

The syntax of using the length property is as follows:

arrobj.length

The following example will demonstrate the use of the length property:

<script language="javascript">
function chklen()
{
       var arr=new Array(1,2,3,4,5)
       document.write(arr.length)
}
</script>


The output of the above code is 5.

The prototype property: The prototype property is used to ascribe new properties or methods to the arrays that are created in the currently loaded document.

The syntax of using the prototype property is as follows:

arrobj.prototype

The concat method: The concat method is used to combine two arrays. The combination of two arrays forms a single array object containing the elements of both arrays.

The syntaxes of using the concat method is as follows:

arrobj.concat(arrobj)
arrobj.concat(value1,value2...)


The following examples will demonstrate the use of the concat method:

  1. Concatenation by using the array object:


  2. <script language="javascript">
    function concarr()
    {
           var arr1=new Array(1,2,3,4,5)
           var arr2=new Array("a","b","c","d","e")
           var arr3=arr1.concat(arr2)
           document.write(arr3)
    }
    </script>


    The output of the above code is 1,2,3,4,5,a,b,c,d,e.

  3. Concatenation by passing values in the concat function:


  4. <script language="javascript">
    function concarr()
    {
           var arr1=new Array(1,2,3,4,5)
           var arr3=arr1.concat("a","b","c","d","e")
           document.write(arr3)
    }
    </script>


    The output of the above code is 1,2,3,4,5,a,b,c,d,e.

The join method: The join method is used to separate the elements of an array with a delimiter that is provided as an argument in the parentheses of the method.

The syntax of using the join method is as follows:

arrobj.join("delimiter")

The following example will demonstrate the use of the join method:

<script language="javascript">
function joinarr()
{
       var arr1=new Array(1,2,3,4,5)
       var arr2=arr1.join(">")
       document.write(arr2)
}
</script>


The output of the above code will be 1>2>3>4>5.

The pop method: The pop method is used to delete the last element in an array.

The syntax of using the pop method is as follows:

arrobj.pop()

The following example will demonstrate the use of the pop method:

<script language="javascript">
function poparr()
{
       var arr1=new Array(1,2,3,4,5)
       arr1.pop()
       document.write(arr1)
}
</script>


The output of the above code will be 1,2,3,4.

The push method: The push method is used to add an element or an object at the end of an array.

The syntax of using the push method is as follows:

arrobj.push(value)
arrobj.push(object)


The following examples will demonstrate the use of the push method:

  1. To push a single value:

    <script language="javascript">
    function pusharr()
    {
           var arr1=new Array(1,2,3,4,5)
           arr1.push(6)
           document.write(arr1)
    }
    </script>


  2. The output of the above code is 1,2,3,4,5,6.

  3. To push multiple values:

    <script language="javascript">
    function pusharr()
    {
           var arr1=new Array(1,2,3,4,5)
           arr1.push(6,7,8,9,10)
           document.write(arr1)
    }
    </script>


  4. The output of the above code is 1,2,3,4,5,6,7,8,9,10.

  5. To push an object:


  6. <script language="javascript">
    function pusharr()
    {
           var arr1=new Array(1,2,3,4,5)
           var arr2=new Array(6,7,8,9,10)
           arr1.push(arr2)
           document.write(arr1)
    }
    </script>


    The output of the above code is 1,2,3,4,5,6,7,8,9,10.

  7. To push multiple objects:


  8. <script language="javascript">
    function pusharr()
    {
           var arr1=new Array(1,2,3,4,5)
           var arr2=new Array(6,7,8,9,10)
           var arr3=new Array(11,12,13,14,15)
           arr1.push(arr2,arr3)
           document.write(arr1)
    }
    </script>


    The output of the above code is 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15.

The reverse method: The reverse method is used to arrange the elements of an array in descending order.

The syntax of using the reverse method is as follows:

arrobj.reverse()

The following example will demonstrate the use of the reverse method:

<script language="javascript">
function revarr()
{
       var arr=new Array(1,2,3,4,5)
       arr.reverse()
       document.write(arr)
}
</script>


The output of the above code is 5,4,3,2,1.

The shift method: The shift method is used to delete the first element of an array.

The syntax of using the shift method is as follows:

arrobj.shift()

The following example will demonstrate the use of the shift method:

<script language="javascript">
function shiftarr()
{
       var arr=new Array(1,2,3,4,5)
       arr.shift()
       document.write(arr)
}
</script>


The output of the above code is 2,3,4,5.

The slice method: The slice method is used for extracting a contiguous series of elements in an array.

arrobj.slice(startindex [, endindex)

The following examples will demonstrate the use of the slice method:

  1. In the following example, the endindex argument will not be used:


  2. <script language="javascript">
    function slicearr()
    {
           var arr=new Array(1,2,3,4,5,6,7,8,9,10)
           var arr1=arr.slice(2)
           document.write(arr1)
    }
    </script>


    The output of the above code will be 3,4,5,6,7,8,9,10.

  3. In the following example, the endindex argument will be used:


  4. <script language="javascript">
    function slicearr()
    {
           var arr=new Array(1,2,3,4,5,6,7,8,9,10)
           var arr1=arr.slice(2,6)
           document.write(arr1)
    }
    </script>


    The output of the above is 3,4,5,6.

The sort method: The sort method is used to arrange the elements of an array in ascending order.

arrobj.sort()

The following code will demonstrate the use of the sort method:

<script language="javascript">
function sortarr()
{
       var arr=new Array(5,4,3,2,1)
       arr.sort()
       document.write(arr)
}
</script>


The output of the above code is 1,2,3,4,5.

The unshift method: The unshift method is used for adding an element at the beginning of an array.

The syntax of using the unshift method is as follows:

arrobj.unshift(valueorobject)

The following examples will demonstrate the use of the unshift method:

  1. To unshift a single value:

    <script language="javascript">
    function unshiftarr()
    {
           var arr1=new Array(1,2,3,4,5)
           arr1.unshift(6)
           document.write(arr1)
    }
    </script>


  2. The output of the above code is 6,1,2,3,4,5.

  3. To unshift multiple values:

    <script language="javascript">
    function unshiftarr()
    {
           var arr1=new Array(1,2,3,4,5)
           arr1.unshift(6,7,8,9,10)
           document.write(arr1)
    }
    </script>


  4. The output of the above code is 6,7,8,9,10,1,2,3,4,5.

  5. To unshift an object:


  6. <script language="javascript">
    function unshiftarr()
    {
           var arr1=new Array(1,2,3,4,5)
           var arr2=new Array(6,7,8,9,10)
           arr1.unshift(arr2)
           document.write(arr1)
    }
    </script>


    The output of the above code is 6,7,8,9,10,1,2,3,4,5.

  7. To unshift multiple objects:


  8. <script language="javascript">
    function unshiftarr()
    {
           var arr1=new Array(1,2,3,4,5)
           var arr2=new Array(6,7,8,9,10)
           var arr3=new Array(11,12,13,14,15)
           arr1.unshift(arr2,arr3)
           document.write(arr1)
    }
    </script>


    The output of the above code is 6,7,8,9,10,11,12,13,14,15,1,2,3,4,5.


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.