This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 6 years ago.
I created a simple function and I noticed something unexpected about javascript. When I pass a variable in to a function, then change it, its not changing outside that function. Heres some code:
function check(val, isEven) {
if (val % 2 === 0){
isEven++;
console.log('incremented isEven is ', isEven);
}
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var isEven = 0;
for (var x = 0; x < arr.length; x++) {
check(arr[x], isEven);
console.log('isEven is now ', isEven);
}
Fiddle is here.
Maybe I've been misinterpreting Javascript all these years, but I would have expected the isEven within check() to be the same as the original isEven.....But you can see in the logs, outside isEven stays as 0....
In JavaScript, objects are passed by copying the reference to the object. Primitive types (string/number/etc) are passed by value.
This means if you passed in an object, modifying the object within the function would be reflected outside the function as inside and outside would both have references to the same object.
With a primitive type, the variable is copied when it is passed in and changes inside the function will no longer be reflected in the variable outside the function.
Related
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 7 months ago.
In this example, taken from the W3Schools JS Tutorial, how is it possible for the variable n to still be used in the next function ?
// Home Made Iterable
function myNumbers() {
let n = 0;
return {
next: function() {
n += 10;
return {value:n, done:false};
}
};
}
// Create Iterable
const n = myNumbers();
n.next(); // Returns 10
n.next(); // Returns 20
n.next(); // Returns 30
I'm coming from a C/C++ background and the lifetime of this variable makes no sense to me. It seems to behave like a global static variable but it should have been deleted when exiting myNumbers(), especially when declared with the let keyword. How can, then, the next method uses it without raising a ReferenceError and keeping its value updated ?
Because of a closure feature of the JavaScript, keep the variable accessible even if the containing function died, whenever there is a child function still need to access it.
For more resources.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 1 year ago.
I am learning javascript and I read that in javascript array and objects are by default passed as a reference. So when I do this:-
var a = [2, 3, 4]
function pushEl(a, num) {
a.push(num)
}
pushEl(a, 5)
console.log(a)
The output is as expected which is
[2,3,4,5]
But what I am not able to understand is that when I assign value to an array or an object inside the function the original array/object is not changed.
var a = [2, 1, 3]
function change(a) {
a = [1, 2]
}
change(a)
console.log(a)
I expect the output to be [1,2] but the output is [2,1,3].
If the array is passed by reference then the changes should have been reflected in the original array too.
Can anybody tell me what concept I'm missing here?
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
JS function not updating an object
(5 answers)
Closed 1 year ago.
Imagine two situations below: We have an array which is a global variable and it will be updated after a function. On the other hand as you can see in commented code my other global variable doesn't updated after a function?
I learnt about pass by reference and value, but i am wondering how come array has been passes by reference to the function but "a" variable has been passes as value?
var a=[1,2,3,4,5];
function adder(arrey, item) {
arrey.push(item);
return arrey.shift();
}
console.log(a);
console.log(adder(a,6));
console.log(a);
var a =10
function adder (num) {
num+=1;
return num;
}
console.log(a);
adder(a);
console.log(a)
This question already has answers here:
Why does this return 3, 1?
(5 answers)
Closed 5 years ago.
Consider the below JavaScript code:
var x = 3;
var foo = {
x: 2,
baz: {
x: 1,
bar: function() {
return this.x;
}
}
}
var go = foo.baz.bar;
alert(go());
alert(foo.baz.bar());
The two alerts here show different values. First one gives "3" and second one "1". But aren't both actually referring the same thing? I think its somehow related to the scope, but I am not getting it how the code is executing here.
Context (basically this) is not defined by where a function is (in contrast to scope) but rather how it is called. Namingly when you do:
object.method()
method is called in objects context. When you do
method()
method is called in no context at all ( or in non strict mode it is window for some reason). However you can change that by binding the context:
var method = object.method.bind(object);
method();
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 8 years ago.
While some people are saying that this question has been answered before, its only the answer that has been given before to a different question with much more specifics (ie. asking specifically about call-by-reference / call-by-value, whereas this question does not presuppose knowledge of eiither)
The following code appears to be very confusing. We can logically deduce that the reason why z.id updates after the function is because it is an object. But why? What particular trait or characteristic of javascript is doing this?
function changea(a) {
a = 100; // does not change a
} // no return
function changez(z) {
z.id = 100; // does change z
} // no return
var a = 0; // a is zero
changea(a) // a variable
alert('variable a is equal to ' + a); // why does this stay at zero?
var z = {id: 0};
changez(z);
alert('variable z.id is equal to ' + z.id); // why does this change to 100
see the demo here: http://jsfiddle.net/u0pysgjy/7/
In the first case a is passed by value to the changea function and in the second case z is an object and it is passed by reference to the changez function.Whenever a function gets a variable which is passed to it by value, even if you change the value it will not reflect outside of the function which is the case of a. And if a function gets its parameter which is passed to it by reference if you change the value of its property it will be reflected outside of the function also.
There is a nice explanation here