1.11M Members

newb Js question about function return global variable reasssignment

 
0
 

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

 
0
 

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);
 
0
 

Geez, don't I feel like a total dumbfrigg now. :D Thank you!

 
0
 

Please make this thread solved, if everything's ok now.

Question Answered as of 1 Year Ago by gon1387 and stbuchok
 
0
 

Marked. :)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article