To handle several pieces of data at once, JavaScript offers two essential structures: arrays and objects.
Arrays
An array stores several values in an ordered list:
const fruits = ["pomme", "banane", "cerise"];
console.log(fruits[0]); // "pomme"
You reach each item through its index, which starts at 0. You can go through an array with a loop or with the forEach method.
Objects
An object groups related information as key: value pairs:
const personne = {
prenom: "Marie",
age: 25
};
console.log(personne.prenom); // "Marie"
- An array is perfect for a list of similar items.
- An object describes a single entity through its properties.
Key takeaway: an array orders a list (index starting at 0), an object describes one thing through its properties (key: value).