# Setting a variable in HubL

**URL:** <https://community.hubspot.com/t/setting-a-variable-in-hubl/1277>\
**Category:** CMS Development\
**Created:** [March 13, 2017, 5:46pm UTC](https://community.hubspot.com/t/setting-a-variable-in-hubl/1277 "2017-03-13T17:46:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wnell](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wnell/32/58551_2.png) [@wnell](https://community.hubspot.com/u/wnell)\
**Post date:** [March 13, 2017, 5:46pm UTC](https://community.hubspot.com/t/setting-a-variable-in-hubl/1277/1 "2017-03-13T17:46:08Z")

</div>

I am not sure if this is by design or a bug in your implementation but it certainly does not seem to be consistent. I have narrowed it down. The below snippet works as expected:

`{% set queryparam2 = "initial" %} {% if 1 == 1 %} {% set queryparam2 = queryparam2 ~ "&type=111" %} {% endif %} {{queryparam2}}`

Above prints “initial&type=111” as expected, meaning one can change a variable’s value inside an if block. However, introducing a for loop:

```auto
{% set queryparam2 = "initial" %} 
{% if 1 == 1 %} 
 {% set queryparam2 = queryparam2 ~ "&type=111" %} 
 {% for part in [1] %}
 {% set queryparam2 = queryparam2 ~ "&type=222" %} 
 {% endfor %} 
{% endif %} 

{{queryparam2}}

```

Above prints “initial&type=111” which is NOT as expected, it should be “initial&type=111&type=222”, meaning one can NOT change a variable’s value inside a for block and access it outside that block, even with the variable defined outside the scope of the for block. Comments?

---

<div class="post-metadata">

**Author:** ![ndwilliams3](https://avatars.discourse-cdn.com/v4/letter/n/c4cdca/32.png) [@ndwilliams3](https://community.hubspot.com/u/ndwilliams3)\
**Post date:** [March 14, 2017, 4:34pm UTC](https://community.hubspot.com/t/setting-a-variable-in-hubl/1277/2 "2017-03-14T16:34:50Z")

</div>

It’s a known limitation. Variables within a for loop are only accessible within the for loop.

---

<div class="post-metadata">

**Author:** ![ndwilliams3](https://avatars.discourse-cdn.com/v4/letter/n/c4cdca/32.png) [@ndwilliams3](https://community.hubspot.com/u/ndwilliams3)\
**Post date:** [March 14, 2017, 4:37pm UTC](https://community.hubspot.com/t/setting-a-variable-in-hubl/1277/3 "2017-03-14T16:37:15Z")

</div>

Can you explain what you are trying to do? Maybe there is a workaround.

---

<div class="post-metadata">

**Author:** ![wnell](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wnell/32/58551_2.png) [@wnell](https://community.hubspot.com/u/wnell)\
**Post date:** [March 14, 2017, 5:20pm UTC](https://community.hubspot.com/t/setting-a-variable-in-hubl/1277/4 "2017-03-14T17:20:35Z")

</div>

Thanks for the info. That is what I thought.

I did implement a workaround but it does make life a bit harder.
