HTML Academy
Using a variable as an index
Arrays8/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
Let’s hit arrays with loops!
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Array length

Have you already guessed what we are going to do next? To loop through arrays!

We know what the initial index of the array is. It equals zero. Indices in the array increase by one. You can use a variable as an index.

It turns out that we can set a variable to zero, and then use it in a loop to access the elements of the array, increasing the index by one at each iteration.

There is only one question: when to stop the loop? To do this, you need to know what the array length is.

And here, too, everything is excellent. Arrays have a length property. Use it after the array’s name to find its length, which is the number of elements for the arrays shown here:

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

// Logs 4 in the console
console.log(numbers.length);

By accessing length you can get the last element of the array, even if you do not know how many elements it holds:

someBigArray[someBigArray.length - 1];

Let’s analyze this expression. Suppose there are 100 elements in this large array. Its length will be equal to 100. The index of the last, hundredth, element will be 99 because the numbering in arrays starts from zero. Therefore, the index of the last element is calculated by subtracting one from the length.

With the length property, you can flexibly work with any arrays. That is, you can use the same code for calculations for arrays of arbitrary length.

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
  1. On the second line, log the length of usersByDay to the console.
  2. On the next line, log the last element of the array in the console: usersByDay[usersByDay.length - 1].
  3. On the next line, log the third-to-last array element to the console, using square brackets and length.
  4. Now, in the first line, add the fifth element 1000 to the array usersByDay, separated by a comma and make sure that the console log function is working correctly.

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.