2

I have this code:

var point = 2;
var change = function(){point = 5};

function makeChange(point,change){
   change();
   alert(point);
}

makeChange(point,change);

The idea is to give the user the ability to pass a condition for the value of point before using it in the function. But it doesn't take effect. When I add alert(point) into the change function it alerts 5 but then alerts 2 in the makeChange function.

3
  • You cannot change a variable passed into a function and have that affect the "original" variable Commented Sep 27, 2016 at 5:58
  • 1
    Possible duplicate of Does Javascript pass by reference? Commented Sep 27, 2016 at 5:59
  • Note the local parameter point and the global variable point are two different variables. You are alerting the local point inside makeChange. Basically just change the name of your first parameter to something other than point. Commented Sep 27, 2016 at 6:16

5 Answers 5

1

you can return point in change()

var point = 2;
var change = function(){ return point=5; };

function makeChange(point,change){
 alert(change);
}

makeChange(point,change());
Sign up to request clarification or add additional context in comments.

4 Comments

I don't understand. What's the "point" of passing "point" into makeChange to start with?
Then what is use of initializing point at this point ? point = 5;
user wants to change the point value in change()..so I just did that
@torazaburo there is no point :P But also worth noting that this code is dangerous. It ONLY works because both change and point exist in the same scope and change uses the same variable. If any of those is not true, then it won't work. However, if both are true, then passing both to a separate function is point-less (heh) and it demonstrates lack of understanding of how functions work - the point passed into makeChange is not altered. Any assumption that it's related to the other one is wrong.
0

The problem is because of your parameter having the same name as the global variable point. In the code below:

var point = 2;
var change = function(){point = 5};

function makeChange(point,change){
   change();
   alert(point);
}

The call to change() inside makeChange will assign the local parameter point defined here makeChange(point,change) to 5. Not the global point which will remain as 2.

A easy way to solve this is to not use the same name point in the paramater as the global variable. Otherwise due to scoping JavaScript will think you are taking about the locally defined point inside makeChange:

var point = 2;
var change = function(){point = 5};

function makeChange(Point,Change){
   Change();
}

makeChange(point,change);
alert(point);

Comments

0

You can do something like this when you call function.

makeChange(point,function(){return change})

Comments

0

Your code is doing fine - in fact, if you do a alert(point); right after makeChange(point,change); you'll see that 5 is returned.

The confusing bit is that the number (namely, 2) is passed into the makeChange function by value instead of the reference (namely, point). Therefore, if you alert point inside makeChange, the originally passed value will be given: 2.

Comments

0

I think the problem with this code is confusion between local and global variable.

So when you are passing variable in function makeChange(point,change) its behaving like local variable for function makeChange now in the next line when you calling the change function its changing global point value. and then when you are doing alert since in function priority is local variable so you are getting 2.

You can use return approach for same.

var point = 2;
var change = function(){point = 5 return(point)};

function makeChange(point,change){
   point=change();
   alert(point);
}

makeChange(point,change);  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.