Challenge - 5 Problems
Variable Mastery in Postman
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the value of the environment variable after this script runs?
Consider this Postman test script that sets an environment variable. What will be the value of the environment variable
userId after execution?Postman
pm.environment.set('userId', 12345); pm.environment.set('userId', Number(pm.environment.get('userId')) + 5);
Attempts:
2 left
💡 Hint
Remember that environment variables are stored as strings and you may need to convert them to numbers before arithmetic.
✗ Incorrect
The script first sets the 'userId' environment variable to 12345. Then it retrieves the value (stored as string '12345'), converts it to a number using Number(), adds 5, and sets the variable to the result: 12350.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies a global variable value in Postman?
You want to check if the global variable
token equals abc123 in a test script. Which assertion is correct?Attempts:
2 left
💡 Hint
Global variables are accessed via pm.globals.get()
✗ Incorrect
Global variables in Postman are accessed using pm.globals.get(). Using pm.environment.get() accesses environment variables, which is different.
🔧 Debug
advanced2:00remaining
Why does this script fail to set the collection variable?
This Postman script tries to set a collection variable but it does not appear in the collection variables list after running. What is the issue?
Postman
pm.collectionVariables.set('sessionId', 7890); console.log('Session ID set to:', pm.collectionVariables.get('sessionId'));
Attempts:
2 left
💡 Hint
Check how Postman handles collection variable persistence after setting them in scripts.
✗ Incorrect
Postman allows setting collection variables dynamically, but these changes are only saved in memory during the run. To persist changes, the collection must be saved manually after the run.
🧠 Conceptual
advanced2:00remaining
Which variable scope has the highest priority when accessing variables in Postman scripts?
In Postman scripts, variables can exist in multiple scopes: global, collection, environment, and local. Which scope is checked first when accessing a variable by name?
Attempts:
2 left
💡 Hint
Think about the closest scope to the script execution context.
✗ Incorrect
Local variables have the highest priority because they are defined within the script and override other scopes. Then environment, collection, and global follow in that order.
❓ framework
expert2:00remaining
Which Postman script snippet correctly sets a variable only if it does not already exist?
You want to set an environment variable
userRole to admin only if it is not already set. Which script snippet achieves this correctly?Attempts:
2 left
💡 Hint
Check how Postman returns undefined or null for unset variables and how JavaScript treats falsy values.
✗ Incorrect
pm.environment.get() returns undefined if the variable is not set. The condition !pm.environment.get('userRole') checks for undefined, null, empty string, or other falsy values, so it safely sets the variable only if not set or empty.