Working with Numeric Environment Variables in Postman
Postman environment variables are commonly used to carry values between requests and tests. A practical example is storing a response time and later comparing it with a numeric threshold.
The important detail is type conversion: values retrieved from environment variables may need to be converted explicitly before numeric comparison.
Original Pattern
The workflow used in this article was:
- Store the response-time value in an environment variable.
- Retrieve the variable in a later script.
- Convert the retrieved value with JavaScript's
Number()function. - Perform the numeric comparison.
The original example is preserved here:
# Store response time in environment variable:
postman.setEnvironmentVariable("response_time", responseTime);
# Retrieve response time from environment variable:
var respTime = environment.response_time;
# Convert response time to numeric value and compare:
if (Number(respTime) > 1000) {
// Some Logic
}
# OR
if (Number(environment.response_time) > 1000) {
// Some Logic
}
Version context: The example uses Postman APIs from the period when the article was written, including older environment-variable access patterns. Current Postman scripts use the modern
pm.*API, so verify the current syntax before copying the example into a new collection.
Why Explicit Conversion Matters
A value that looks numeric can still be represented as text. Explicit conversion makes the intent clear and avoids relying on JavaScript's implicit coercion rules, which can produce surprising comparisons.
Takeaway
When values move through configuration or environment-variable layers, treat their type as part of the contract. Convert them explicitly before arithmetic or threshold comparisons so test logic remains predictable.