Update variable inside a function, why isn't it working?

I am trying to update a global variable using a function and I don’t understand why it’s not working! :fearful:

        let allTheMoney = 1000;

        const updateBankroll = (bankroll, dollars) => {
        if (!isNaN(bankroll) && !isNaN(dollars)) {
            console.log('old BR : ' + bankroll) // this prints 1000
            bankroll = parseInt(bankroll);
            dollars = parseInt(dollars);
            bankroll += dollars;
            console.log('new BR : ' + bankroll) // this prints 1050
            return bankroll;
         } else {
            return 'We need numbers here'
        }
        }

        updateBankroll(allTheMoney, 50);

        console.log('after function : ' + allTheMoney) // this acts like the function didn't change anything and prints 1000

Can anyone tell me what I am doing wrong?

One of the main reasons is the allTheMoney variable is not getting reassigned.

If we walk through the function you’re having. We have two args: bankroll and dollars.
You update the bankroll arg based on the value of dollars. Then you return bankroll.

Now at the bottom when you call the function you’re never using the return value of it. you’re just giving the updateBankRoll function two arguments and that’s it.
As a side note please have in mind that here when you pass allTheMoney you’re passing it by value and not reference. This means JavaScript copies that value and uses it in function without touching the actual variable.

I hope the information above cleared some of the confusion and can lead you to the right direction. If not please let us know to get into more detail and explain things further.

Also just to get the title right technical wise, the allTheMoney variable is not really a global variable here. It’s just a variable with broader scope compared to the function you’re using. I can suggest reading a bit more about the concepts of scoping and var,let,const to get that deeper knowledge about these.

Either way good luck! :call_me_hand:

1 Like

Thank you @shervinee, it is not 100% clear in my mind how ‘return’ works (and when to use it) but I get the part about passing the value and not the reference and now it makes sense!

And I will definitely read more documentation about scoping (for now I really just use ‘let’ as default for all my variables and ‘const’ for my functions but I don’t really know why :sweat_smile: )

1 Like

Oh no worries at all. Let me tell you that it’s great that you’ve decided to use those cause it can clear some confusion you may have along the way.
Scoping and using the right variables may seem a bit too much at first but it’s really not that bad.
Glad I could clear things a little about the pass by reference vs value!
And as for the return you can just think of at as the output of a function, it may have it or it may not have it. But it’s fine if you don’t want to get into details of it now because I’d say one new topic at a time!

Good luck on solving this and keep up the good work!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.