JavaScript – Arrays

JavaScript – Arrays

JavaScript Arrays

An array is a way of storing list of data

 

Defining Arrays

Array Constructor

var arrayname = new Array(); creates empty array

var student = new Array(5);  creates array with 5 items

It is not necessary to set the length of an array, it can vary as needed during script execution.

Values can be assigned as array is declared

var student = new Array(“aaa”,”bbb”,”ccc”);

 

Javascript allows arrays to have values of different data types

var answers = new Array(10,”Yes”,21.23);

 

Arrays Literal Notation

Array literal notation provides a shorter method of defining arrays rather than using constructor.

var student = [];

var student = [“aaa”,”bbb”,”ccc”];

var answers = [10,”Yes”,21.23];

 

Accessing Arrays

Array Index is used to access array elements. The array index starts with 0 index.

answer[0] is 10

answer[1] is “Yes”

 

Length property

The length property is used to obtain the length of an array

student.length will give 3

for(var i=0;i<student.length;i++)

Changing Values:

student[1] = “zzz”;

 

Changing Length:

var student = [“aaa”,”bbb”,”ccc”];

student.length = 2;

array originally had 3 items but now has 2 items

var student = [“aaa”,”bbb”,”ccc”];

student.length = 5;

The array will now have 5 items, and last two items are undefined until values are assigned.

 

Properties of Arrays

constructor

index

length

 

Methods of Arrays

join() Method

join([separator]);

var student = [“aaa”,”bbb”,”ccc”];

var msg = student.join(“,”);   result aaa,bbb,ccc

 

pop() method is used to remove last item from an array, and return last item

var student = [“aaa”,”bbb”,”ccc”];

student.pop();

student = [“aaa”,”bbb”];

 

push() method is used to add items at the end of an array

var student = [“aaa”,”bbb”,”ccc”];

student.push(“zzz”); student.push(“zzz”,”yyy”,”xxx”);

var student = [“aaa”,”bbb”,”ccc”,”zzz”];

 

shift() method is used to remove first item from an array and return the value

 

unshift() method is used to add items to the beginning of an array

 

reverse() method is used reverse the order of items in an array

 

sort() method converts each array item in to string value and the arrange items in ascending order based on string values

 

concat() method is used to concatenate

var student = [“aaa”,”bbb”,”ccc”];

var staff = [“xxx”,”yyy”];

var all = student.concat(staff);  [“aaa”,”bbb”,”ccc”,”xxx”,”yyy”];

 

var student = [“aaa”,”bbb”,”ccc”];

var staff = [“xxx”,”yyy”];

var all = student.concat(staff,“zzz”,”111”];

result [“aaa”,”bbb”,”ccc”,”xxx”,”yyy”,”zzz”,”111”];

 

slice() method is used to slice specific portion of an array

arrayname.slice(start,stop); start is index were starts slice and stop is index after the last item you want to slice

 

var student = [“aaa”,”bbb”,”ccc”,”xxx”,”yyy”];

var result = student.slice(1,3);                    result = [”bbb”,”ccc”];

 

splice() method removes or replaces items

var student = [“aaa”,”bbb”,”ccc”,”xxx”,”yyy”];

student.splice(2,1);  result = [“aaa”,”bbb”,”xxx”,”yyy”];

var student = [“aaa”,”bbb”,”ccc”,”xxx”,”yyy”];

student.splice(2,1,”zzz”);  result = [“aaa”,”bbb”,”zzz”,”xxx”,”yyy”];

 

indexOf() method

var student = [“aaa”,”bbb”,”ccc”,”xxx”,”bbb”];

indexOf(“ccc”) returns 2

 

lastIndexOf() method

var student = [“aaa”,”bbb”,”ccc”,”xxx”,”bbb”];

student.lastIndexOf(“bbb”) returns 4

 

Nesting of Arrays

var student = [  [“aaa”, 60],

[”bbb”, 70],

[”ccc”, 80]

];

 

var student = new Array(  new Array(“aaa”, 60),

new Array(”bbb”, 70),

new Array(”ccc”, 80)

);

 

Accessing

var i = 0;

var j = 0;

for(i=0;i<student.length;i++)

{

for(j=0;j<student[i].length;j++)

{

student[i][j];

}

}