# collectionVariables returns value by reference or by value

**URL:** <https://community.postman.com/t/collectionvariables-returns-value-by-reference-or-by-value/55592>\
**Category:** Help Hub\
**Tags:** variables, collections\
**Created:** [December 12, 2023, 2:15pm UTC](https://community.postman.com/t/collectionvariables-returns-value-by-reference-or-by-value/55592 "2023-12-12T14:15:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 12, 2023, 3:01pm UTC](https://community.postman.com/t/collectionvariables-returns-value-by-reference-or-by-value/55592/2 "2023-12-12T15:01:07Z")

</div>

Hey @vivek5080-05 👋

Welcome to the Postman Community! :postman:

In your first request, it’s using `pm.variables.set()` which only lives for the duration of that single request. It’s not storing those values in a variable anywhere.

The `pm.variables.get()` in your second request would go pick the variable with that name, which is in the narrowest scope and use that - It wouldn’t be using anything from the first request.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/1/612504c835a942bae42cac0bb8d1af687150077a.jpeg)

> **[Store and reuse values using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/#variable-scopes)**
>
> Store and reuse values using variables: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

I would personally just stick to using `collectionVariables` for it all, that way you’re not getting tripped up by the way that `pm.variables.get()` works in Postman.

This is from a slightly different context but uses the `.shift()` method in the script that @michaelderekjones shared, It might be worth taking a look at.

> [@Asserting over a dynamic boolean](https://community.postman.com/t/asserting-over-a-dynamic-boolean/54784/11):
>
> Here is a basic example of using setNextRequest. I’m running this against Postman Echo with a very basic request body. [image] With the following in the pre-request scripts… if (!pm.collectionVariables.get("booleans")) { pm.collectionVariables.set("booleans", JSON.stringify([])); } let array = JSON.parse(pm.collectionVariables.get("booleans")); if (array.length === 0) { array = [true, false]; } pm.test("boolean array is not empty", () =\> { pm.expect(array).to.be.an("array").th…

---

_[View the full topic](https://community.postman.com/t/collectionvariables-returns-value-by-reference-or-by-value/55592)._
