Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Object-Oriented JavaScript
  • Table Of Contents Toc
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)
close
close
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)

Overview of this book

Table of Contents (18 chapters)
close
close
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
2
Built-in Functions
4
Regular Expressions
5
Index

Array


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

3

>>> 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

[3.14]

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

Property/Method

Description

length

The number of elements in the array.

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

4

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

Merges arrays together.

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

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

join(separator)

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"

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

"1|2|3"

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

"1 is less than 2 is less than 3"

pop()

Removes the last element of the array and returns it.

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

"trois"

>>> 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');

4

reverse()

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]

shift()

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

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

1

>>> a

[2, 3]

slice

(start_index,

end_index)

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"]

sort(callback)

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'); 

5

>>> a 

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

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Object-Oriented JavaScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon