Arrays in javascript


Hi

Array is very important concept in JavaScript.Here i m including the basis concept of JavaScript Arrays

What is an array in JavaScript ?
An Array is a special variable, which can hold more than one value at a time
Ways of creating arrays in javascript
An array can be created in 3 ways
1. Regular way

var empName = new Array(5);
empName[0] = “Ram”;
empName[1] = “Mohan”;
empName[2] = “Hari”;
empName[3] = “Chandradev”;
empName[4] = “Rajan”;

2. Condensed Way

var empName = new Array(“Ram”, “Mohan”, “Hari”,”Chandradev”);

3. Literal Way

var empName = [“Ram”, “Mohan”, “Hari”,”Chandradev”];

Way of Access Arrays
Var name= empName[0];

In array there are so many inbuilt function
Like
1.Push
2.Pop
3.shift
4.Slice
5.Sort
6.Concate
7.Join
8. toStrings

Sample code for Slice function

//for Slice functionality
function ArrayTestSlice() {
var empName = [“Ram”, “Mohan”, “Arjun”, “Chandradev”];
var sEmpName = empName.slice(1);
var s1EmpName = empName.slice(1, 3);
alert(sEmpName); // output: Mohan,Arjun,Chandradev
alert(s1EmpName); // output: Mohan,Arjun

}

Complete code for Array in Javascript


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArrayTest.aspx.cs" Inherits="Javascript_Test_ArrayTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        //First ways 
        function ArrayTestRegular() {
            var empName = new Array(5);
            empName[0] = "Ram";
            empName[1] = "Mohan";
            empName[2] = "Hari";
            empName[3] = "Chandradev";
            empName[4] = "Rajan";
            for (var i = 0; i < empName.length; i++) {
                // console.log(empName[i]);
                //alert(empName[i]);
                document.writeln(empName[i]);
            }
        }

        //Second ways
        function ArrayTestLiteralWay() {
            var empName = ["Ram", "Mohan", "Hari","Chandradev"];
            empName.sort();
            for (var i = 0; i < empName.length; i++) {
               // alert(empName[i]);
                document.writeln(empName[i]);
            }
        }

        // 3rd Ways
        function ArrayTestCondensed() {
            var empName = new Array("Ram", "Mohan", "Hari", "Chandradev");
            empName.sort();
            for (var i = 0; i < empName.length; i++) {
                 alert(empName.valueOf());
               // document.writeln(empName[i]);
            }

        
            
        }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnClick" runat="server" OnClientClick="ArrayTestCondensed();return false;" Text="Submit" /> 
    </div>
    </form>
</body>
</html>


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: