How share a variable between to functions?

In the first function, I have trolleyArray variable.I m taking an list when response returns . So this variable is an array and setting as trolleyArray variable. In the second function I want the call trolleyArray variable. But taking undifined error. How share my trolleyArray variable between to functions? If we have solution,could u write a response on my sample code

export const options = {
    scenarios: {
        CreatePickingStartedSalesOrders: {
            executor: 'per-vu-iterations',
            exec: 'CreatePickingStartedSalesOrders',
            vus: 1,
            iterations: 1,
            startTime: '0s'
        },
        CreatePicking: {
            startTime: '45s',
            exec: 'CreatePicking',
            executor: 'shared-iterations',
            vus: 2,
            iterations: 1,
            maxDuration: '50s',
        },
    }
}

var trolleyArray;
export function CreatePickingStartedSalesOrders(data) {
    let createPickingStartedSalesOrdersHeader = {
        headers: {
            "api": "testtt",
            "Content-Type":"application/json"
        },
    };

    describe('PickingInProgress', (t) => {
        let createSalesOrdersRes = http.post(requestUrlSeed + 'CreateOrders', JSON.stringify({
            salesOrderConfigurations: [
                {
                referencePrefix: 'XL-',
                flow: 'MultiItem',
                }
                ],
                tenant: 'SpeedyLojistik',
                operation: 'Rossmann'
        }), createPickingStartedSalesOrdersHeader)
        trolleyArray= createSalesOrdersRes.json()
        t.expect(createSalesOrdersRes.status).toEqual(200);
    });

}

export function CreatePicking(data) {

    let generateTokenHeader = {
        headers: {
            'Authorization': `Bearer ${data.access_token}`,
            'content-type': 'application/json',
        },
    };

    describe('Picking', (t) => {

      
        let continueRes = http.post(requestUrlAPI + 'PickingProcess.continueExist', JSON.stringify({
            trolleyLabel: trolleyArray[exec.vu.idInTest]
        }), generateTokenHeader
     
        t.expect(continueRes .status).toEqual(200);
        sleep(1)

Hi Yusuf,

It seems to me that this answer will help you. You may also have a look at this

@PaulM Hi paul thanks for reply but I didn’t understand that examples. Could u give me a sample on my code

As far as i know the global variable is just a copy inside a function. When we add value in var trolleyArray in function CreatePickingStartedSalesOrders this values are visible just inside this fuction. Function CreatePicking does’t see it.
Because every function creates its own virtual machine.
Maybe this link helps you.

Thanks Paul, simple global variable sharing should not be hard like this . This is very interesting.

To me, this looks like one scenario because CreatePicking [looks like it] depends on CreatePickingStartedSalesOrders. As one scenario you would not have an issue sharing local variables inside the one function, or invoke the other function from the first function and pass the trolleyArray as a parameter to it.