Start New Discussion within our Web Development Community

Have a look at this snippet:

var changeNumber = function(number)    {
    return number + 5;
};

var newNumber = 4;
changeNumber(newNumber);
alert(newNumber);

I've been taught that the function should reassign whatever value it returns to the global variable NewNumber, so that newNumber should change from 4 to 9. This doesn't, however, seem to happen when I test-run the code; newNumber retains its original value. So where'd I go wrong?

Prog

Hi proglearner,
You forget to catch the value. :)

Your code should have been done like this:

var changeNumber = function(number){
    return number + 5;
};
var newNumber = 4;
// Should catch the returned value from changeNumber function
newNumber = changeNumber(newNumber);
alert(newNumber);
This question has already been answered. Start a new discussion instead.