1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Cycle Through an Array of Values with the Modulus Operator

    reactReact

    We often need to move in one direction or another in a JavaScript array to focus on one value at a time. When we hit an upper or lower bound, it's convenient to have the referenced index jump to the other end of the array so we can keep cycling through in that same direction. In this lesson, we'll apply this to a React UI, allowing the UI to "jump" to the beginning of the array when we hit "next" at the end of the array, and to jump to the last item in the array when moving "back" and we hit the lower bound of the array and we'll do it by taking advantage of the % (modulus) operator.