What kind of variable is it?

Hi everyone
I’m not good in the theory. So I’ve got a question about variables.
There are variables:

firstVar = "value 1"
pm.variables.set("firstVar", "value 2")
pm.variables.set("secondVar", "value 3")

console.log(firstVar)
console.log(pm.variables.get("firstVar"))
console.log(pm.variables.get("secondVar"))

Following postman docs pm.variables.set() sets a local variable.
But I haven’t found the name of variable without pm.variables.set().
It works differently. I get how to work ith it but I want to be better in the theory.

Tell me please what kind of variable is it?

Hey @marigen00 :wave:

Welcome to the Postman Community! :postman:

There are a number of different variable scopes within Postman and their usage depends on the context.

When you set a variable value using pm.variables.set , the value is local and will only persist for the current request or collection run. That value doesn’t get stored anywhere and cannot be reused outside of that request.

When using pm.variables.get() it’s slightly different, that will search through each variable scope (global, collection, environment, data, local(using pm.variables.set())) and return the variable in the narrowest scope, if it has the same name.

For example, saving a variable with the same name to the Collection and Global scope, would log the Collection variable to the console as it has the narrowest scope.

pm.collectionVariables.set("name", "danny");
pm.globals.set("name", "danny");

console.log(pm.variables.get("name"));

I’m more than happy to expand on the information here if you have follow up questions.

Thank you for your answer with pm.****(). You helped to correct my question.
What is the scope of variables without postman/pm. commands?
Is it also local variable but in JS scope?
Or am i wrong?

Those are the standard JS declared variables (var, let, const) and can be used within the sandbox.

For more detailed information around those, I would recommend taking a look over this documentation:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.