JavaScript Programming/Arrays

From Wikiversity
Jump to navigation Jump to search

This lesson introduces JavaScript arrays.

Objectives and Skills[edit | edit source]

Objectives and skills for this lesson include:[1]

  • Declare and use arrays
    • Single-dimensional arrays; multi-dimensional arrays; iteration; initialization; define an array; sort and search an array; use push, pop, shift, and unshift methods; use the length property; access an array element;

Readings[edit | edit source]

  1. Wikipedia: Array data type
  2. Wikipedia: Dynamic array
  3. Wikibooks: JavaScript/Arrays

Multimedia[edit | edit source]

  1. YouTube: JavaScript Tutorial for Beginners - 17 - Arrays
  2. YouTube: JavaScript Arrays with different data types
  3. YouTube: How to Create Arrays in JavaScript
  4. Youtube: JavaScript Tip: 7 Ways to Iterate Over an Array
  5. YouTube: Push, Pop, Unshift and Shift Array Methods
  6. YouTube: Multidimensional array tutorial

Examples[edit | edit source]

Activities[edit | edit source]

Complete the following activities using external JavaScript code. Apply JavaScript best practices, including comments, indentations, naming conventions, and constants. Use input elements for input and respond to input or change events or add buttons and respond to click events. Use HTML elements for output. Use separate functions for each type of processing. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.

  1. Extend any of the activities from JavaScript Programming/Loops to use an array to hold the information generated during processing (values only) and display the results from the array (add HTML formatting) after processing is complete.

Lesson Summary[edit | edit source]

  • The first element of the array has an index of 0. The second element has an index of 1. The last element has an index of arrayName.length - 1.[2]
  • JavaScript arrays are used to store multiple values in a single variable.[2]
  • An array that has multiple arrays in it is called a multi-dimensional array.[source?]

Key Terms[edit | edit source]

array
Arrays allow us to store multiple values in a single variable.[3]
array methods
Built-in functions to work with arrays in JavaScript.[4]
index or subscript
An integer that defines an element position within an array and is used to access that element.[2]
length
The number of elements in an array.[5]
multi-dimensional array
An array whose elements consist of other arrays.[6]
pop() method
This method removes the last element from an array.[7]
push() method
Is the easiest way to add a new element to an array.[2]

See Also[edit | edit source]

References[edit | edit source]

  1. Microsoft: Exam 98-382 Introduction to Programming Using JavaScript
  2. 2.0 2.1 2.2 2.3 W3Schoolsː JavaScript Arrays
  3. https://www.w3schools.com/js/js_arrays.asp
  4. https://www.w3schools.com/js/js_array_methods.asp
  5. https://www.w3schools.com/jsref/jsref_obj_array.asp
  6. https://www.dyn-web.com/javascript/arrays/multidimensional.php
  7. "JavaScript Array Methods". www.w3schools.com. Retrieved 2020-10-13.