# Can I reuse a JavaScript variable across requests?

**URL:** <https://community.postman.com/t/can-i-reuse-a-javascript-variable-across-requests/21436>\
**Category:** Help Hub\
**Tags:** javascript\
**Created:** [March 16, 2021, 5:52pm UTC](https://community.postman.com/t/can-i-reuse-a-javascript-variable-across-requests/21436 "2021-03-16T17:52:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TeacherManAlex](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@TeacherManAlex](https://community.postman.com/u/TeacherManAlex)\
**Post date:** [March 16, 2021, 5:52pm UTC](https://community.postman.com/t/can-i-reuse-a-javascript-variable-across-requests/21436/1 "2021-03-16T17:52:51Z")

</div>

Is there any way to preserve my Javascript environment across requests in a collection run? I need to use javascript to extract an ID in one request, and then use that ID in the next request in the run.  
I can do that using environment variables, and that’s fine.

However, what I next want to do is to push the ID numbers onto an array, and then pop them off in a later request. That means preserving the array across requests.

Is there any way to do that in JavaScript? The only way I can see is to serialize the array into a string, store it into an environment variable, and then deserialize it in a later request.

Any ideas?

---

<div class="post-metadata">

**Author:** ![hannah.neil](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/hannah.neil/32/4477_2.png) [@hannah.neil](https://community.postman.com/u/hannah.neil)\
**Post date:** [March 16, 2021, 10:03pm UTC](https://community.postman.com/t/can-i-reuse-a-javascript-variable-across-requests/21436/2 "2021-03-16T22:03:52Z")

</div>

Hey there, welcome to the community!

I think I get the gist of your question but a few more clarifying details would be helpful. [Here’s](https://community.postman.com/t/how-to-best-ask-questions-on-community-to-get-the-answers-you-need/16912) some best practices around getting the answers you need.

Using the environment variables is how I’d recommend doing that. Can you clarify what you mean by “pop them off in a later request” ? Not sure what pop off means 🙂

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [March 17, 2021, 11:29am UTC](https://community.postman.com/t/can-i-reuse-a-javascript-variable-across-requests/21436/3 "2021-03-17T11:29:25Z")

</div>

Hey @TeacherManAlex,

Your idea is correct, the pattern I typically put in place for this type of operation is:

```auto
const myArray = pm.environment.has('myArray') ? JSON.parse(pm.environment.get('myArray') : [];

const responseData = pm.response.json();
myArray.push(responseData.myValue);

pm.environment.set('myArray', JSON.stringify(myArray));

```

Then in the consuming request, I would do exactly as you suggested:

```auto
const myArray = JSON.parse(pm.environment.get('myArray'));
const myVariable = myArray.pop();

// Do work with the variable

pm.environment.set('myArray', JSON.stringify(myArray));

```

@hannah.neil, ‘pop’ refers to taking the last item from an array 🙂

---

<div class="post-metadata">

**Author:** ![hannah.neil](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/hannah.neil/32/4477_2.png) [@hannah.neil](https://community.postman.com/u/hannah.neil)\
**Post date:** [March 17, 2021, 3:30pm UTC](https://community.postman.com/t/can-i-reuse-a-javascript-variable-across-requests/21436/4 "2021-03-17T15:30:56Z")

</div>

Ah, that makes sense. Adding that to my vocabulary. Thanks for chiming in!
