HTML Academy
A lot of data? An array!
Arrays6/30
Back to the list of tasks
  • 1. Little Muffin and Big Data
  • 2. Average value
  • 3. Finishing the traffic analyzer
  • 4. New data
  • 5. A lot of data? An array!
  • 6. Reading an array element by index
  • 7. Using a variable as an index
  • 8. Array length
  • 9. Let’s hit arrays with loops!
  • 10. Summation in the loop
  • 11. Finishing refactoring
  • 12. One small analytical investigation
  • 13. Writing to an array by index
  • 14. The murderer is a butler!
  • 15. Summary of “Arrays”. Part 1
  • 16. Fifth program: Beginner decryptor
  • 17. Vague suspicions
  • 18. Swapping the elements
  • 19. Looking for the minimum element
  • 20. The minimum element is found!
  • 21. Starting sorting
  • 22. Continue sorting
  • 23. Finish sorting
  • 24. Testing the sorting
  • 25. Median of an odd number of elements
  • 26. Median of an even number of elements
  • 27. Green light
  • 28. The murderer is the butler, again!
  • 29. Summary of “Arrays”. Part 2
  • 30. Sixth program: Long jump records
Using a variable as an index
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Reading an array element by index

We will continue refactoring a bit later. And now we will learn how to work with arrays.

An array is a single list of values, and therefore it can be stored in one variable and this variable can be passed to some command, for example, to muffin.plot(). Isn’t it much more convenient than passing a bunch of individual values to a command?

When processing an array, you often need to get its individual values. To get an array element, you need to specify the index of this element in square brackets after the array’s name.

var numbers = [1, 2, 3, 4, 5];

// Logs 2 in the console
console.log(numbers[1]);

But why was 2 logged, and not 1? The numbering of elements in the array starts with a zero: the first element of the array has index zero, the second has index one, the third has index two and so on.

By the way, if you find it difficult to remember such an unusual way of numbering, you can use the age analogy: age counting starts from zero. If a person is 25 years old, it means that they are in their 26th year of life. From 2000 to 2004, Michael Schumacher won five championship titles in a row.

Let’s sum it all up. To get the first element of the array, for example, usersByDay, you need to write usersByDay[0]. The variable name identifies the array we are accessing. The square brackets indicate that we do not need the entire array, but only one of its elements. We put the index of the element we need inside the square brackets.

Let’s try reading a few more entries from the array usersByDay.

Comments

  • script.js
JavaScript
var usersByDay = [812, 1360, 657, 1247];

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted

Let’s get the values ​​from the array.

  1. After the array declaration, declare firstDayUsers and assign it the first array element: usersByDay[0].
  2. On the next line, log variable firstDayUsers in the console.
  3. On the next line, declare a variable fourthDayUsers and write into it the fourth element of the array: usersByDay[3].
  4. On the next line, log variable fourthDayUsers in the console.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.