Implementing Arrays


Arrays: Arrays are use for store multiple data, data types and values in one variable.

Syntex: There are two ways to implementing arrays
Way 1:
var arrayName = new Array("array1","array2",..."array n");
Way 2:
var arrayName = ["array1","array2",..."array n"];
We can implied arrays by using [] Big braces or in other meaning [] braces indicate to arrays.

As example if i want to store data with name of 30 students than i use make new var for 30 times but by using of array we can store 30 names in one var name, let see an example:

var studentName = ["ankit", "jane", "janne", "jack","mac" ,"jhon"];
console.log(studentName);

In above example we create an array with var studentName and store all students names in it and than display array on console screen by using console.log command we can also use an alert command.

One more example : Store multi data types into array.

var myArray = ["Hello", 1, 2, true]; //here we store one string two integers and one boolean data type
console.log(myArray);

Note: In every arrays list index counting start with zero.

How can change the array value by using their index.

var myArray = ["Hello", 1, 2, true];
myArray[0] = "Hi there!"; //change the value of index 0.
console.log(myArray); //display Hi there! 1 2 true

Array push function: This function is use for add an new array in last index of array list.

myArray = ["rohan","nimit"];
myArray.push("ankit"); //Add an new array on last number of array list.
console.log(myArray); // rohan nimit ankit

Array pop function: This function is use for Remove last item of array list.

myArray = [1,2,3,4,5];
myArray.pop(); //remove last one item of array list.
console.log(myArray); // 1 2 3 4

Unshift function : Add an new array in starting in arrays in list

myArray = [2,3,4,5];
myArray.unshift(1); //add one item in starting in array list.
console.log(myArray); // 1 2 3 4 5

Shift function : Remove first item from array list

myArray = [1,2,3,4,5];
myArray.shift(); //remove first item from array list.
console.log(myArray); // 1 2 3 4 5

Concatenation all arrays list together.

myArray = [1,2,3]; // array list 1
myArray1= ["Hello people", 10, 20, false], // array list 2
myArray2= ["Hello public", 30, 50, true,false]; // array list 3

var myArray = myArray.concat(myArray1).concat(myArray2); // join all array list in array list 1

Join function: This function also same as concat function but we can also use a special sign with joining.

myArray = [1,2,3]; // array list 1
myArray1= ["Hello people", 10, 20, false], // array list 2
myArray2= ["Hello public", 30, 50, true,false]; // array list 3

var myArray = myArray.concat(myArray1).concat(myArray2);
var joined = myArray.join("!--");
console.log(joined);

Reverse function: Display all arrays in revers or last to first.

myArray = [1,2,3];
myArray.reverse();
console.log(myArray); // 3 2 1

Sort function : Display array list in alphabetical order.

myArray = ["d","e","g","b","a","f","h","c"];
myArray.sort();
console.log(myArray); // a b c d e f g h


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting