Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Introduction to Arrays

In the lesson on conditions, you taught your programs to make decisions. This lesson is about storing many values together.

Imagine you want to keep the names of five students. With what you know so far, you would need five variables:

const student1 = "Jennifer";
const student2 = "Boris";
const student3 = "Nancy";
const student4 = "David";
const student5 = "Ava";

That's a lot of typing. And what if a sixth student joins the class? Every new student would need a new variable. This is what arrays are for. An array is a list of values stored in one variable:

const students = ["Jennifer", "Boris", "Nancy", "David", "Ava"];

The values in an array are called its elements. This array has five elements, and they keep their order: "Jennifer" is the first, "Ava" the last.

You print an array with console.log(), like any other value. The console shows it the way it is written in code: in square brackets, with text elements in quotes, so that you can see where each element starts and ends:

const fruits = ["apple", "banana", "cherry"];
console.log(fruits);

This prints:

["apple", "banana", "cherry"]

Instructions

  1. 1

    Print the array students: on the next line, write console.log(students);. The console should show ["Jennifer", "Boris", "Nancy", "David", "Ava"].

Back

1/11

  1. 1. Introduction to Arrays
  2. 2. Creating Arrays
  3. 3. Accessing Array Elements
  4. 4. Changing Array Elements
  5. 5. Length and the Last Element
  6. 6. Adding Elements: push()
  7. 7. Removing Elements: pop()
  8. 8. Searching an Array
  9. 9. Nested Arrays
  10. 10. Characters of a Text
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.