Book Image

Object-Oriented JavaScript

By : Stoyan Stefanov, Stoyan STEFANOV
Book Image

Object-Oriented JavaScript

By: Stoyan Stefanov, Stoyan STEFANOV

Overview of this book

Table of Contents (18 chapters)
Object-Oriented JavaScript
About the Author
About the Reviewers
Built-in Functions
Regular Expressions


The Array constructor creates array objects:

>>> var a = new Array(1,2,3);

This is the same as the array literal:

>>> var a = [1,2,3]; //recommended

When you pass only one numeric value to the Array constructor, it's assumed to be the array length. An array with this length will be created, and filled with undefined elements.

>>> var a = new Array(3);
>>> a.length


>>> a

[undefined, undefined, undefined]

This can sometimes lead to some unexpected behavior. For example, the following use of the array literal is valid:

>>> var a = [3.14]
>>> a


However, passing the floating-point number to the Array constructor is an error:

>>> var a = new Array(3.14)

invalid array length

Members of the Array Objects




The number of elements in the array.

>>> [1,2,3,4].length


concat(i1, i2, i3,...)

Merges arrays together.

>>> [1,2].concat([10,20], [300,400])

[1, 2, 10, 20, 300, 400]


Turns an array into a string. The separator parameter is a string and the default value is a comma.

>>> [1,2,3].join()


>>> [1,2,3].join('|')


>>> [1,2,3].join(' is less than ')

"1 is less than 2 is less than 3"


Removes the last element of the array and returns it.

>>> var a = ['une', 'deux', 'trois'];
>>> a.pop()


>>> a

["une", "deux"]

push(i1, i2,i3,...)

Appends elements to the end of the array and returns the length of the modified array.

>>> var a = [];
>>> a.push('zig', 'zag', 'zebra','zoo');



Reverses the elements of the array and returns the modified array.

>>> var a = [1,2,3];
>>> a.reverse()

[3, 2, 1]

>>> a

[3, 2, 1]


Like pop() but removes the first element, not the last.

>>> var a = [1,2,3];
>>> a.shift();


>>> a

[2, 3]




Extracts a piece of the array without modifying the source array.

>>> var a = ['apple', 'banana','js', 'css', 'orange'];
>>> a.slice(2,4)

["js", "css"]

>>> a

["apple", "banana", "js", "css", "orange"]


Sorts an array. Optionally accepts a callback function for custom sorting. The callback function receives two array elements as arguments and should return 0 if they are equal, 1 if the first is greater and -1 if the second is greater.

An example of a custom sorting function that does a proper numeric sort (since the default is character sorting):

function customSort(a, b){
  if (a > b) return 1; 
  if (a < b) return -1; 
  return 0;

Example use of sort():

>>> var a = [101, 99, 1, 5];
>>> a.sort();

[1, 101, 5, 99]

>>> a.sort(customSort);

[1, 5, 99, 101]

>>> [7,6,5,9].sort(customSort);

[5, 6, 7, 9]

splice(start, delete_count, i1, i2, i3,...)

This is probably the most powerful of the array functions. It can remove and add elements at the same time. The first parameter is where to start removing, the second is how many items to remove and the rest of the parameters are new elements to be inserted in the place of the removed ones.

>>> var a = ['apple', 'banana','js', 'css', 'orange'];
>>> a.splice(2, 2, 'pear', 'pineapple');

["js", "css"]

>>> a

["apple", "banana", "pear", "pineapple", "orange"]

unshift(i1, i2, i3,...)

Like push() but adds the elements at the beginning of the array as opposed to the end. Like shift() but adds to the array as opposed to removing from it. Returns the length of the modified array.

>>> var a = [1,2,3]; 
>>> a.unshift('one', 'two'); 


>>> a 

["one", "two", 1, 2, 3]