Arrays

Objectives

JavScript arrays

Array-like notation and behavior

JavaScript also uses the array subscript notation as an option for referencing the properties of objects. In this case, the subscripts are strings. This looks and acts like what is called an associative array or a map in other languages. Technically, even though the notation and usage may look like a JavaScript array in some ways, it is not a JavaScript array.

Multi-dimensional arrays in JavaScript

Multidimensional arrays in JavaScript are created by having the elements of an array be arrays themselves. For example, to create a two dimensional array that looks like a Tic-tac-toe board and contains the numbers 1 through 9, do the following:

var board = new Array(); for (var row=0; row<3; row++) { board[row] = new Array(); for (var col=0; col<3; col++) { board[row][col] = row * 3 + col + 1; } }

Converting between arrays and strings

var arr = ["up", "down", "strange", "charm", "bottom", "top"]; // the following displays: up:down:strange:charm:bottom:top console.log(arr.join(":")) var bigbang = "Leonard, Sheldon, Penny, Howard, Rajesh, Bernadette, Amy"; var arr2 = bigbang.split(", "); // the following displays each character name on a separate line for (var i=0; i<arr2.length; i++) console.log(arr2[i]);

Array demonstration programs