create variable in postman code example

Example 1: how to define variables in api

-Use pm.globals to define a global variable:

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

-Use pm.collectionVariables to
define a collection variable:

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

-Use pm.environment to define an environment
variable (in the currently selected environment):

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

-You can use unset to remove a variable:
pm.environment.unset("variable_key");

-Defining local variables
Local variables are temporary values you set
in your request scripts using the following syntax:

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

Example 2: Postman Variables

just like variables in Java. 
we can create variables in postman to reuse the value 
1.Global variables: accessible everywhere in same workspace 
2.Environment variables: accessible in selected environment 
3.Collection Variables: belong to the collection itself and 
only accessible within the collection

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");