# What is the difference between initializing variables using var vs pm.variables.set?

**URL:** <https://community.postman.com/t/what-is-the-difference-between-initializing-variables-using-var-vs-pm-variables-set/26642>\
**Category:** Help Hub\
**Tags:** variables, scope\
**Created:** [August 11, 2021, 10:26am UTC](https://community.postman.com/t/what-is-the-difference-between-initializing-variables-using-var-vs-pm-variables-set/26642 "2021-08-11T10:26:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stelpstra](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@stelpstra](https://community.postman.com/u/stelpstra)\
**Post date:** [August 11, 2021, 10:26am UTC](https://community.postman.com/t/what-is-the-difference-between-initializing-variables-using-var-vs-pm-variables-set/26642/1 "2021-08-11T10:26:44Z")

</div>

Hi,

In a collection with multiple requests, if I create a variables like this in Pre-requests Scripts or Tests of one of the requests:

```auto
var myVar1 = "I am created using var";
pm.variables.set("myVar2", "I am created using pm.variables.set");

```

I have seen both methods been used and I searched what the differences would be but I couldn’t find any useful explanations. I hoped to find it here [https://learning.postman.com/docs/sending-requests/variables/#variable-scopes](https://learning.postman.com/docs/sending-requests/variables/#variable-scopes), but that is not described.

Writing this post I see a message my question might have answers on [https://testing-one-2-three.postman.co/home](https://testing-one-2-three.postman.co/home), but I can’t seem to access that.

What is the difference between using var and pm.variables.set and when would you use which method?

Thanks, Jaap

---

<div class="post-metadata">

**Author:** ![andekis](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@andekis](https://community.postman.com/u/andekis)\
**Post date:** [March 29, 2022, 11:09pm UTC](https://community.postman.com/t/what-is-the-difference-between-initializing-variables-using-var-vs-pm-variables-set/26642/2 "2022-03-29T23:09:37Z")

</div>

I also want to know this.

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [March 29, 2022, 11:15pm UTC](https://community.postman.com/t/what-is-the-difference-between-initializing-variables-using-var-vs-pm-variables-set/26642/3 "2022-03-29T23:15:07Z")

</div>

Hi everyone!

`var` or `let` are [standard variable definitions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) in Javascript.

These are stored in code (in memory) only and do not persist after a run and are limited in scope.

For data to persist outside of a run or use a variable in additional requests, you will need to use Postman variables which is what `pm.variables.set` and `pm.variables.get` does.

You can learn more about using Variables in Postman [here](https://learning.postman.com/docs/sending-requests/variables/).

Hope this helps! 😄
