Linked Questions
154 questions linked to/from Does JavaScript pass by reference?
1811 votes
36 answers
589k views
Is JavaScript a pass-by-reference or pass-by-value language?
The primitive types (number, string, etc.) are passed by value. Still, objects are unknown because they can be both passed by value (in which case we consider that a variable holding an object is a ...
8 votes
3 answers
23k views
How to negate bool inside function in JS? [duplicate]
I'm writing some script now and I have a problem when trying to negate boolean inside a function. I mean this: var test = true; function changeThisBoolPlease(asd){ asd=!asd; } alert(test); ...
3 votes
1 answer
8k views
Toggle boolean using function? [duplicate]
I would like to create a single function to toggle any boolean of my code, but I'm not able to get the expected result : function toggle(x){ x=!x; } var test=false; toggle(test); alert(test); Why ...
-1 votes
4 answers
6k views
JavaScript, changing a variables value inside of a function when the variable is passed as a parameter [duplicate]
Why doesn't this work and is there any way to get it to work? Im pretty sure it doesn't work because of variable scoping in javascript, but I can't think of any ways to get it to work. Code var x = 5; ...
-1 votes
3 answers
3k views
Javascript - map and callback - returning values are not affected [duplicate]
I want to use map method on array with callback to another function. Everything seems to be working fine, but at the end returning values are not affected. I don't know what seems to be the problem. ...
0 votes
1 answer
5k views
How do you assign a value inside a forEach loop? [duplicate]
I expected the first console.log to be [0,10,20,30] and the second to be [0,100,20,30]: var arr = [0,10,20,30] console.log(arr) arr.forEach(each) console.log(arr) function each(data,index) {...
4 votes
4 answers
222 views
Need to understand Javascript object references [duplicate]
I am looking at this piece of code from John Resig's website. What I don't understand is when the ninja object is set to an empty object, the yell method is still available to samurai. Is it ...
1 vote
2 answers
1k views
JavaScript push an object variable into array occur that value changed [duplicate]
An example, I declared an object json and in a for loop, I change the json's value and push it into the list2, But It didn't output the value what I expected. var json = { value: '', text: '',...
0 votes
1 answer
759 views
Async Function: Variable Mutation works, Reassignment Doesn't. Why? [duplicate]
This is more of a curiosity thing than a code not working thing. It would seem that when using Async Functions in JS, mutating passed in variables works as one would expect, while reassigning them ...
1 vote
1 answer
319 views
Can someone explain why this code print "Wild" in javascipt? [duplicate]
Hi I have a question about this code, why does it prints "Wild" instead of "Tabby" var cat = { name: "Athena" }; function swap(feline) { feline.name = "Wild"; feline = ...
0 votes
0 answers
485 views
Javascript Changing one object also changes the other object [duplicate]
In my code snippet I have an object "cat" that I store into two variables "Pelle" and "Kalle". If I change the name of Pelle, it also changes the name of Kalle. I believe it has to do with them being ...
2 votes
5 answers
133 views
Changing a global variable in an function JS [duplicate]
I have looked online about changing variables in an function but still doesn't work. I'm trying to change the global variable in a function. An object with numbers are multiplied by each other. The ...
0 votes
2 answers
188 views
JavaScript: Calling Array.reduce when outer function is called, and outer function has arguments [duplicate]
var foo = 0; var bar = 0; var arr1 = [1, 2, 3, 4, 5]; var arr2 = [6, 7, 8, 9, 10]; function getSum(arr, sum) { sum = arr.reduce(function(accum, val) { return accum += val; }, 0) } ...
2 votes
2 answers
96 views
Javascript reference vs value - arrays [duplicate]
Since arrays are passed to functions by reference, when I set the array to be equal to something else inside a function, and try to log it outside of the function again, why does the value remain the ...
2 votes
3 answers
79 views
Unable store the objects into an array in javascript [duplicate]
I'm trying to store the objects into Javascript array. var res=[ {name:'Alex',place:'US',age:20}, {name:'Jason',place:'Canada',age:25} ]; var obj={}; var data=[]; for ( let i in res){ ...