Reference Vs Value In JavaScript
Why Stack Overflow recommends it
Stack Overflow users link it when someone is confused about why JavaScript variables or function arguments change (or don't change) after assignment or a function call.
Recommended by 1 people across 1 different months, 2019 to 2024.
What it is
A short tutorial from Web Dev Simplified explaining pass by reference versus pass by value in JavaScript. It walks through a visual model of memory addresses, then live-codes examples showing how objects, arrays and function arguments behave.
What people said
Recommends Mina · 2024-05 · Stack Overflow · on “function gives equal value before and after changing a variable”
@Ujjawal, If you still don't understand, I suggest watching this video [this video] to have a full understanding on what is the difference between reference and value.
Cites Phantom · 2019-08 · Stack Overflow · on “Having trouble understanding this in Javascript”
I was watching this video that talks about Reference Vs Value In JavaScript. [this video] I was really stuck on his last example here. let c = [1,2] //0x01 console.log('c = ${c}') add(c,3) console.log('c = ${c}') function add(array,element){ array = [element] } The output is c = 1,2 c = 1,2 I am trying to understand why that is. Based on my understanding, the add function should allocate [element] to a second address (let's say 0x02), then reassign c to 0x02. Which means that the value of c is now the memory address 0x02, hence c = [3]. What is it that I am not understanding correctly?
Cites VMMOORTHY · 2022-02 · Stack Overflow · on “Argument values change on run time in JavaScript”
function func1(a, b) { let args = arguments; console.log(args); return function () { for (let i in args) // it runs 2 times { console.log(args[i], a, b) args[i] += (a + b); // first time args[0] (this is the reference to 'a') set to 2+2+4 = 8 // second time args[1] (it is the reference to b) set to 4+8+4 = 16 console.log("|") console.log(args[i], a, b); console.log("end") } }}func1(2, 4)(); args[0] refers to a and args[0] refers to b because args is a object that is stored is somewhere else in browser/node environment refer how reference acts in js using this video [this video]