What you need to consider is "where" the variable information is to be stored. This may also be referred to as the variable scope.
1. Global - across entire application.
2. Document - specific to current document.
3. Format - specific to current formatting pass.
4. Page - specific to current page.
5. Element - specific to current tag.
Your #1 is Global-scope, your #2 is Element-scope and your #3 is also Global-scope and indeed scoped at the o/s process level too. APP provides "counters" which are Format-scope and they tend to be used for what I think you are attempting.
You will also find the Global-scope Javascript variables also act as Format-scope when they are used inside formatting tags. However this behaviour is only for scalar variables, not for objects or arrays.