www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons JavaScript Array Functions Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Lesson 45
Lesson 46
Lesson 47
Lesson 48
Lesson 49

Due to the power of arrays, JavaScript comes ready-made with a number of useful functions for working with arrays. In this lesson we will discuss the most useful of these. In this tutorial we use the terms function and method, interchangeably.

Before exploring array functions please note that the array functions are invoked using the '.' operator as follows:

myArray.arrayFunction();

concat()
We use the concat() method to concatenate two or more arrays, or a series of values with an array. Code example follows:

fruits = ["Banana", "Grape"];
moreFruits = ["Mango", "Orange"];
document.write(fruits.concat(moreFruits));


The above code would output the elements of both the arrays.

join()
The join() function converts all values in an array to strings and then joins them together into one larger string, placing an optional seperator between elements.

games = ["cricket", "football", "hockey"];
document.write(games.join("-"));


Without a parameter, join would use a comma to separate the array elements.

push() and pop()
We use the push() method to add an element to the end of an array. On the other hand the pop() method is used to remove an element from the end of an array. Following is an example of their use:

seasons = ["summer", "winter", "autumn"];
seasons.push("spring");

colors = ["white", "blue", "red"];
colors.pop();


reverse()
We use the reverse method to reverses the order of all the elements in an array.

vegitables = ["onion", "garlic", "potato", "tomato"];
vegitables.reverse();


sort()
We use the sort function to sort an array.

fruits = ["banana", "mango", "apple", "orange"];
fruits.sort();

Array .length property
The length property of any array returns the count of the elements that are in the array:

fruits = ["banana", "mango", "apple", "orange"];
document.write(fruits.length); // would output 4

Generally we use the length property of an array in loops to iterate over the whole array:

for (i = 0; i < fruits.length; i++) {
//array manipulation code goes here
}

Next >>> Lesson No. 18: JavaScript Objects and Classes







Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.