variable types postman code example

Example 1: postman environment variables

pm.environment.set("variable_key", "variable_value");

Example 2: collection variables postman

Only accessible within the same collection and
can not be accessed anywhere else
COLLECTION variables are available throughout 
the requests in a collection and are independent
of environments, so do not change based on the selected environment.
I use collection variables when
I use single environment like url details.
We can refer to the variable by using {{}}(double curly brace)
and  it can be used anywhere

Example 3: defining local variable in api

Local variables are temporary values you set in your request 
scripts using syntax :

pm.variables.set("variable_key", "variable_value");