Arrays and objects

suggest change
const myArray = []; // empty array

An array is an ordered list of values. For example:

const favoriteFruits = ["apple", "orange", "strawberry"];
const carsInParkingLot = ["Toyota", "Ferrari", "Lexus"];
const employees = ["Billy", "Bob", "Joe"];
const primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31];
const randomVariables = [2, "any type works", undefined, null, true, 2.51];

let myArray = ["zero", "one", "two"];
window.alert(myArray[0]); // index 0 holds "zero"
myArray = ["John Doe", "Billy"];
const elementNumber = 1;

window.alert(myArray[elementNumber]); // Billy

An object is a group of named values. Unlike an array, you look those values up by name rather than by index:

const myObject = {};
const john = {firstname: "John", lastname: "Doe", fullname: "John Doe"};
const billy = {
    firstname: "Billy",
    lastname: undefined,
    fullname: "Billy"
};
window.alert(john.fullname); // John Doe
window.alert(billy.firstname); // Billy

Rather than making an array ["John Doe", "Billy"] and calling myArray[0], we can just call john.fullname and billy.fullname.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents