Module 21: Arrays and Objects

Lesson 1 of 5 · 15–25 minutes

Running JavaScript with Node.js, and arrays

🏠The everyday picture

Until now your JavaScript lived inside a web page, like a cook who only works in one restaurant's kitchen. Node.js gives the same cook a kitchen of their own. And an array is the cook's numbered row of jars.

📘What it really is

const products = ["Notebook", "Pen", "Bag"];
console.log(products[0]);       // Notebook
console.log(products.length);   // 3
console.log(products[products.length - 1]);  // Bag, the last item

🖱️Try it

  1. Install Node.js (LTS) and check node --version in a new terminal.
  2. Make a folder level-3, open it in VS Code, run git init, and create catalogue.js.
  3. Put the code above in it and run node catalogue.js.
  4. Add a fourth product and print the new last item.

✅Quick check

An array has 5 items. What's the index of the last one?

Show the answer
  1. Indexes start at 0, so the last is length - 1.

Can you add an item to an array declared with const?

Show the answer

Yes. const means the name always points to the same array; its contents can still change.

← BackModule 21
ProLearners Assist