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

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:

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, 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, 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

I also want to know this.

Hi everyone!

var or let are standard variable definitions 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.

Hope this helps! :smile:

1 Like