Arrays
• An array is a special variable, which can hold more than one
value at a time.
var dog1= “poodle";
var dog2 = “beagle";
var dog3 = “pomeranian";
Array elements
Array name
var dog = [“poodle“, “beagle“, “pomeranian"] ;
Index 0 index 1 index 2
• Using an array literal is the easiest way to create a JavaScript
Array.
• Syntax
A. Using array literals
var array_name = [item1, item2, ...];
B. Using new keyword
var cars = new Array(
"Saab",
"Volvo",
"BMW");
• You access an array element by referring to
the index number.
• Array indexes start with 0.
• [0] is the first element. [1] is the second element.
var cars =
["Saab", "Volvo", "BMW"];
cars[0] = "Opel";
var cars =
["Saab", "Volvo", "BMW"];
document.writeln(cars);
• Arrays are a special type of objects. The typeof
operator in JavaScript returns "object" for
arrays.
• But, JavaScript arrays are best described as
arrays.
• Arrays use numbers to access its "elements".
• Objects use names to access its "members". In
this example, person.firstName returns John
var person = {firstName:"John",
lastName:"Doe",
age:46};
• JavaScript variables can be objects. Arrays are
special kinds of objects. Because of this, you can
have variables of different types in the same
Array.
• You can have objects in an Array. You can have
functions in an Array. You can have arrays in an
Array:
array[0]=currentDate.getDate(); //objects
array[1]= name(); //functions
array[2]= fruits;//array
• The real strength of JavaScript arrays are the
built-in array properties and methods:
1. length;
2. toString();
3. join()
4. pop()
5. push()
6. splice()
7. slice()
8. sort()
9. reverse()
• The length property of an array returns the
length of an array (the number of array
elements).
• The JavaScript method toString() converts an
array to a string of (comma separated) array
values.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits.toString());
• The join() method also joins all array elements
into a string.
• It behaves just like toString(), but in addition
you can specify the separator:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write(fruits.join(“ * ”));
• The pop() method removes the last element
from an array:
• The push() method adds a new element to an
array (at the end):
• The splice() method can be used to add new
items to an array:
The first parameter (2) defines the position where new elements
should be added (spliced in).
The second parameter (0) defines how many elements should
be removed.
The rest of the parameters ("Lemon" , "Kiwi") define the new elements
to be added.
The splice() method returns an array with the deleted items:
• The slice() method slices out a piece of an array
into a new array.
The slice() method can take two arguments like slice(1,
3).
The method then selects elements from the start
argument, and up to (but not including) the end
argument.
• The sort() method sorts an array alphabetically:
• The reverse() method reverses the elements in
an array.
• You can use it to sort an array in descending
order:
• By default, the sort() function sorts values as
strings.
• This works well for strings ("Apple" comes before
"Banana").
• However, if numbers are sorted as strings, "25" is
bigger than "100", because "2" is bigger than "1".
• Because of this, the sort() method will produce
incorrect result when sorting numbers.
• You can fix this by providing a compare function:
• Use the same trick to sort an array descending:
To create a two-dimensional array:
END of DISCUSSION