Why array object in function automatically get value of global array object? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why array object in function automatically get value of global array object?

I got following code: var getWeird = function(yourArray) { var weirdArray = yourArray; for (var i=0; i<yourArray.length; i++) { yourArray[i]=1; } alert(weirdArray); alert(yourArray); }; var myArray = [1,2,3]; getWeird(myArray); //output //1,1,1 //1,1,1 In the past i was coding in C++, so for me it's very weird behaviour of program.

6th Aug 2017, 6:52 PM
Grz
Grz - avatar
7 Answers
+ 5
I think that your problem here is that objects (and Arrays are object) are passed at argument of function by reference, not by value (copy, as when you pass a String or a Number)... Passing argument by reference, means that the location of variable is passed rather than the value itself (object could be big and will require more memory to be passed by value -- duplicating it). You also copy only the reference of an Array (or more generally an object) when you assign it to a new variable: var myArray = [1,2,3]; var myCopy = myArray; myCopy[1] = 42; alert(myArray[1]); // will ouput 42, because you've accessed to the same array whatever the reference you use ;P Well so, if you wat to work to a real (deep) copy (duplicate values) of your array in your function, you need to use the 'slice()' Array method: var myArray = [1,2,3]; var myDeepCopy = myArray.slice(); myDeepCopy[1] = 42; alert(myArray[1]); // will ouput 2, because you've accessed two different arrays now ;)
7th Aug 2017, 7:38 AM
visph
visph - avatar
+ 3
it gets global value because your declared var myArray gets hoisted to the top by the JS compiler but not it's value so when the JS engine looks up the value of yourArray it's declared at the top but it's value is found inside the function( JS engine starts searching for the value from the current scope in this case from the function scope) https://code.sololearn.com/WkIXZP2wR87E/?ref=app
7th Aug 2017, 1:55 AM
Lord Krishna
Lord Krishna - avatar
+ 2
@Grz when the var is first declared inside the function the compiler assigns the value of yourArray to weird array, then the for loop starts. PS: commented the "use strict" check if it works now// it works on Solo app code play ground PPS: the language is not interpreted but compiled. unlike c++ whose compiler has few minutes to some hours at best . the JS engine has milliseconds to seconds at best. think it this way would you wait 10 minutes for best optimised code or a few seconds when loading a JS website. https://en.m.wikipedia.org/wiki/Chrome_V8
7th Aug 2017, 6:24 AM
Lord Krishna
Lord Krishna - avatar
+ 2
@Grz try to share the code and run it on latest version of chrome or Firefox.
7th Aug 2017, 6:36 AM
Lord Krishna
Lord Krishna - avatar
+ 1
Thank you very much for answer Lord Krishna! So it means that js interpreter firstly make code from loop and then assign a value to weirdArray? Ps. I can't run your code, i got illegal invocation error.
7th Aug 2017, 5:52 AM
Grz
Grz - avatar
+ 1
thank you very much for answer visph :-)
7th Aug 2017, 8:32 AM
Grz
Grz - avatar
0
I tried to run your code on solo learn app on my phone and still i get illegal invocation at line 16.
7th Aug 2017, 6:35 AM
Grz
Grz - avatar