0

I am doing substitution of values in an array as part of my attempt to learn JavaScript coding. As I take the original array and console.log the output, then do my substitution and again console.log the output, I get the same result from both console.log results (e.g. the value of 67 in the original array has been replaced by 73 in both console.log outputs).

Code section in question -

var myNumericalArray = [23,45,67,91];
console.log(myNumericalArray);
myNumericalArray[2] = 73;
console.log(myNumericalArray);
myNumericalArray = [];
console.log(myNumericalArray);

Console.log output result -

Array(4) 0:23 1:45 2:73 3:91 length: 4__proto__: Array(0)

Array(4) 0:23 1:45 2:73 3:91 length: 4__proto__: Array(0)

Array(0)length: 0__proto__: Array(0)

I would have thought the first output would have been 23,45,67,91 and the second one 23,45,73,91. The third console.log output returns an empty array as I would have expected. In fact, I put the third step in under the premise that it would refresh any browser stored values in case that was the reason the 73 showed up in the first array output values; like from previously running the code.

This really is about understanding the processing flow steps and clearing of values in a relatively simple array situation. I have a much more complex code flow calling HTML input form results, grabbing and using function values to pass the values to an API putting METAR aviation weather where the code flow has frustrated me. So I decided to see if I could understand the logic in a much more basic case presented here and I am still stymied in terms of figuring out how initial values are cleared for an updated value to be presented.

Can anyone offer insight on this question around JavaScript flow processing to explain why this result is occurring.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Blackhawk
  • 13
  • 4
  • The array is being modified as desired – CertainPerformance Feb 16 '20 at 02:49
  • Once the substitution is run it 'is' being modified as desired. The question is why is the substitution showing up in the output from the first console.log statement which I would have thought was before the substitution would have taken place. – Blackhawk Feb 16 '20 at 13:26
  • It is very strange. The code actually runs exactly as expected in the "Run Code Snippet" above. Where I am getting the odd substitution in both the first instance (unexpected) as well as the second (expected) is when running it in an Opera browser and with the code in between in the HEAD section of the file. And the Opera browser is set to run as a VPN. – Blackhawk Feb 16 '20 at 20:00
  • I ran the code in a Safari Browser and the result was the same. However, the console.log in Safari noted that in each of the three, the variables were global. Thus, I am wondering if the answer to my question is that the substituted array being global is why the console.log output for the first already is showing the array has been substituted. However, if the sequence is thus impacted, what I don't get is why then, the third step emptying the array doesn't produce empty result for all three array output console.logs? – Blackhawk Feb 20 '20 at 15:15
  • Don't think that the console shows your object as it is *at the moment* you log it. The console is asynchronous, and looks at the array at its own time, potentially when you have already modified it. – trincot Feb 20 '20 at 15:23
  • For me the code sample is just working as expected by the code – john Smith Feb 20 '20 at 15:41
  • Thanks Trincot. That makes perfect sense. After further looking up the logic of asynchronous processing, I am even further informed on the why. With JavaScript's front-end role, you never know what actions or sequence a user might take with the front-end step they invoke, so being able to handle and thus produce result from any order makes asynchronous processing totally logical if I had only thought about it. I appreciate the insight as a new user. – Blackhawk Feb 22 '20 at 16:06

0 Answers0