Values, Types, and Expressions
Arrays and Objects
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
xstarts at3.pointbecomes{"x":3,"y":4}.items[1]reads20for the printed item.items[2]is30, sosum = point.x + items[2]becomes33.- The program prints
point={"x":3,"y":4},item=20, andsum=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.