Modifying a variable in a forEach loop

P

Paul Carey

Hi

I would have expected the following to change the value of a, but just
discovered it doesn't.

var forEach = function () {
var a = "a";
[a].forEach(function (v, i) {
console.log("v=" + v);
v = i;
console.log("v=" + v);
});
console.log("a=" + a);
};

The output is:
v=a
v=0
a=a

I'm not really sure why, I'd be very grateful if someone could
explain.
Thanks

Paul
 
H

Henry

Hi

I would have expected the following to change the value
of a, but just discovered it doesn't.

var forEach = function () {
var a = "a";
[a].forEach(function (v, i) {
console.log("v=" + v);
v = i;
console.log("v=" + v);
});
console.log("a=" + a);

};

The output is:
v=a
v=0
a=a

I'm not really sure why, I'd be very grateful if
someone could explain.

Aren't you the person in the best position to explain why you expected
the value of - a - to be changed? Given that a function's parameters
(- v - in this case) have the same status as a function's local
variables (in terms of scoping/visibility) there was never any reason
for expecting assigning to a parameter to have any effect outside of
the function where it happens. And given that Arrays are just
sequences of values there is no reason for expecting a method
operating upon an Array to have any effect on the value of a variable
that was previously read in order to initialise one of its values.
 
P

Paul Carey

Aren't you the person in the best position to explain why you expected
the value of - a - to be changed? Given that a function's parameters
(- v - in this case) have the same status as a function's local
variables (in terms of scoping/visibility) there was never any reason
for expecting assigning to a parameter to have any effect outside of
the function where it happens. And given that Arrays are just
sequences of values there is no reason for expecting a method
operating upon an Array to have any effect on the value of a variable
that was previously read in order to initialise one of its values.

Indeed. My mistake in oversimplifying an example.
 
S

slebetman

Hi

I would have expected the following to change the value of a, but just
discovered it doesn't.

var forEach = function () {
var a = "a";
[a].forEach(function (v, i) {
console.log("v=" + v);
v = i;
console.log("v=" + v);
});
console.log("a=" + a);

};

The output is:
v=a
v=0
a=a

I'm not really sure why, I'd be very grateful if someone could
explain.

In javascript, plain numbers and strings are passed by value. Only
objects are passed by reference. If we modify your code slightly
you'll see the expected behavior:

var forEach = function () {
var a = {value:"a"};
[a].forEach(function (v, i) {
console.log("v",v);
v.value = i;
console.log("v",v);
});
console.log("a",a);
};

output:
v Object value=a
v Object value=0
a Object value=0
 
D

dhtml

slebetman said:
In javascript, plain numbers and strings are passed by value. Only
objects are passed by reference. If we modify your code slightly
you'll see the expected behavior:

Actually, object references are passed by value.

Garrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top