Additional array examples and notes

Objectives

  • Initialize arrays when creating them
  • Modify an array element
  • Display elements of an array on a web page using document.writeln
  • Display elements of an array on a web page using a constructed string and Element.innerHTML
  • Display elements of an array on a web page using DOM techniques
  • Use JavaScript to set the attributes of an element

Initialize arrays and modify elements

  • Creating and initializing an array of 5 numbers: var nums = new Array(1, 6, 2, 9, -3);
  • Creating and initializing an array of 5 strings: var names = new Array("Aaron", "Dave", "Angie", "Pat", "Sam");
  • Modifying the third element of the nums array: nums[2] = 42;

Code to display array elements as a list using document.writeln

Note: This code would be placed in the body element. Example is at arrayDisplay.html

Code to display array elements as a list using Element.innerHTML

Example is at arrayDisplay2.html

Code to display array elements as a list using DOM techniques

Example is at arrayDisplay3.html

Code to display array elements as paragraphs using DOM techniques

Note: This code also demonstrates how to set an attribute of an element. Example is at arrayParas.html