--- title: v-for Range Iteration Starts at 1, Not 0 impact: LOW impactDescription: v-for with a number range starts at 1, unlike JavaScript arrays which start at 0 type: gotcha tags: [vue3, v-for, list-rendering, range] --- # v-for Range Iteration Starts at 1, Not 0 **Impact: LOW** - When using `v-for` with a number (range iteration), the iteration starts at `1`, not `0`. This differs from typical JavaScript behavior where arrays are 0-indexed. This gotcha commonly causes off-by-one errors when the generated numbers are used for calculations or array indexing. ## Task Checklist - [ ] Remember `v-for="n in 10"` produces 1 through 10, not 0 through 9 - [ ] When using range values for array indexing, subtract 1: `items[n - 1]` - [ ] Consider creating a computed array if you need 0-based indices **Incorrect Assumption:** ```html {{ n }}
  • {{ items[n].name }}
  • ``` **Correct:** ```html {{ n }}
  • {{ items[n - 1].name }}
  • {{ index + 1 }}. {{ item.name }}
  • ``` ```html
    Loading placeholder {{ n }} of 3...
    ``` ## When Range Iteration Is Useful - Rendering a fixed number of placeholder/skeleton elements - Creating pagination buttons: `v-for="page in totalPages"` - Generating star ratings: `v-for="star in 5"` - Repeating template structures a set number of times ## Reference - [Vue.js List Rendering - v-for with a Range](https://vuejs.org/guide/essentials/list.html#v-for-with-a-range)