CS133JS Beginning Programming: JavaScript

Arrays: Array Methods

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7.Objects and Object Constructors
3. Conditional Statements: if and switch8. Web Page I/O
4. Repetition: while, do while, and for9. Review
5. Midterm Check-Point10. Term Project
11. Final 

Table of Contents

Introduction

Announcements

Registration is open. These are the spring term classes reccomended for Software Dev majors:

Q and A

What's Due this Week

 

Review

Array Declaration and Initialization

Array Operations

Arrays are not copied by the assignment operator. Only a reference is copied.

 

Arrays and Loops

The for loop is especially suited to working with arrays and the for...of loop is only used with arrays.

for loop

for...of loop

 

2D Arrays

Arrays can contain other arrays (nested arrays).

 


Array Methods (and a Property)

Get Array Information

In the examples below, assume we have defined the array continents as shown below.

 

length

A property that contains the length of an array.

Use this property when you don't know how many elements are in an array, but need to do some operation that requires knowing that.

Example, swapping values in an array

The continents array is shown as a table below with the operations that swap the values in the first and last elements shown to the right of the array.

continents
index   value
operations
0 "Asia" →         →        firstValue
←                           ↓
1 "Africa"          ↑                    ↓
2 "North America"          ↑                    ↓
3 "South America"           ↑                   ↓
4 "Antarctica"          ↑                    ↓
5 "Europe"          ↑                    ↓
6 Australia" → lastValue          ↓
←         ←         ←

 

 

indexOf(...)

A method that returns the index of the element containing a specified value. If the value isn't found, indexOf will return -1.

Use this method when you need to do an operation on a specific element of the array, but only know the value stored in the element and not the index of the element. For example:

 

Modify Array Elements

splice(...)

The splice() method can be used to insert, remove, or replace elements anywhere in an array. It returns the removed elements.

Syntax

array.splice(index, numberToDelete, newValue1, ....., newValueN)

Examples

 

concat(...)

Join two arrays.

Copy an array. This kind of copy is called a deep copy. because it copies all the elements in the array instead of just copying references.

 

Remove Array Elements

pop()

Removes the last element from an array and returns it.

 

shift()

Removes the first element from an array and returns it.

 

Add Array Elements

push(...)

Adds an element to the end of an array and returns the length of the new array.

 

unshift(...)

Adds an element to the beginning of an array and returns the length of the new array.

 

Reference

JavaScript Array Reference on W3Schools

JavaScript Reference: Array on MDN


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird 2018, revised are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.