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 from an array using index
  • 7. Variable as 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
Variable as index
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Reading from an array using index

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

An array is a one-piece list, and therefore it can be written into one variable and this variable can be transferred to some command, for example, to muffin.plot(). Isn’t it much more convenient than transferring a bunch of individual variables to a command?

When processing arrays, you almost always need to get its individual values. To get an array element, you need to specify the ordinal number or the index of this element in the already familiar to us square brackets.

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

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

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

By the way, if you find it difficult to remember such an unusual way of numbering, you can use the year analogy: chronology and age counting also start from zero. If a person is 25 years old, it means that they are on 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 indicates that we are accessing an array. The square brackets indicate that we do not need the entire array, but only one of its elements. We mark the number of the element we need using 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, declare variable firstDayUsers and write into it the first element of the array: 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−2025

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.