# Scope of variable in for loop

**URL:** <https://community.hubspot.com/t/scope-of-variable-in-for-loop/22548>\
**Category:** CMS Development\
**Created:** [July 7, 2020, 12:05pm UTC](https://community.hubspot.com/t/scope-of-variable-in-for-loop/22548 "2020-07-07T12:05:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonp](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/simonp/32/60590_2.png) [@simonp](https://community.hubspot.com/u/simonp)\
**Post date:** [July 7, 2020, 12:05pm UTC](https://community.hubspot.com/t/scope-of-variable-in-for-loop/22548/1 "2020-07-07T12:05:47Z")

</div>

I am trying to number of images created by a module as define by loop.length value

i trying th following:

\<div id=“a”\>  
{% for item in module.image\_gallery %}  
{% set no\_Images = loop.length %}  
\<img src=“{{ item.src }}” alt=“{{ item.alt }}” \>

\<h3\>Loop length with loop = {{ no\_Images }}\</h3\>  
{% endfor %}

\</div\>

\<h3\>Loop length outside of loop= {{ no\_Images }}\</h3\>

But i dont seem to be able to get the value outide of the loop

for now i using jquery to find the length;

var imgCount = $(“img”).length;

---

<div class="post-metadata">

**Author:** ![John](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/john/32/43512_2.png) [@John](https://community.hubspot.com/u/John)\
**Post date:** [July 7, 2020, 12:39pm UTC](https://community.hubspot.com/t/scope-of-variable-in-for-loop/22548/2 "2020-07-07T12:39:18Z")

</div>

try:

`Loop length outside of loop {{ module.image_gallery|length }}`

---

<div class="post-metadata">

**Author:** ![simonp](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/simonp/32/60590_2.png) [@simonp](https://community.hubspot.com/u/simonp)\
**Post date:** [July 7, 2020, 2:12pm UTC](https://community.hubspot.com/t/scope-of-variable-in-for-loop/22548/3 "2020-07-07T14:12:43Z")

</div>

thankls john
