Hold values in an array and an object.

Arrays and Objects

array_object.js
const x = 3;
const point = { x: x, y: 4 };
const items = [10, 20, 30];
const shown = JSON.stringify(point);
const sum = point.x + items[2];

console.log("point=" + shown);
console.log("item=" + items[1]);
console.log("sum=" + sum);

Follow the Values

  1. x starts at 3.
  2. point becomes {"x":3,"y":4}.
  3. items[1] reads 20 for the printed item.
  4. items[2] is 30, so sum = point.x + items[2] becomes 33.
  5. The program prints point={"x":3,"y":4}, item=20, and sum=33. | x | point text | item | sum | | --- | --- | --- | --- | | 1 | {"x":1,"y":4} | 20 | 31 | | 3 | {"x":3,"y":4} | 20 | 33 | | 7 | {"x":7,"y":4} | 20 | 37 |
array-object An array holds values by position and an object holds them by name. Reading an element or field returns a plain value; `JSON.stringify` shows an object as readable text.

Exercise: array_object.js

Reproduce sum=33, then use the pinned x values 1 and 7 to predict the point text and sum.