HTML Academy
Reading an array element by index
Arrays7/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
Array length
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Using a variable as an index

Square brackets can contain an expression, not just a number. JavaScript evaluates the expression to decide which element to access. For the array elements in this chapter, use an expression that gives a non-negative integer index.

Let’s practice this way of accessing array elements and access different elements with the same variable. To do this, you need to declare a variable to store the current index of the array element. Let’s call it index and set its initial value to zero.

If you now log the expression usersByDay[index] in the console, we’ll see the first value of the array. After all, now the variable holds zero, and the expression usersByDay[index] is equivalent to usersByDay[0].

If you change the value of the variable index from zero to one and access usersByDay[index] again, we’ll then get an element with the index 1, that is, the second element.

Look, the expression in both cases is the same: usersByDay[index]. But it returns different values when the value of index changes.

If we continue to increase index and read elements of the array usersByDay, then sooner or later we will go through all the elements.

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 elements of the array by manually changing the index variable.

  1. After the array, declare a variable index with a zero value.
  2. On the next line, log usersByDay[index] in the console.
  3. On the next line, set index to 3.
  4. On the next line, once again log usersByDay[index] 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.