0

Why does the console.log function with GOOGLE CHROME displays the final version of a variable even if the it has not been changed yet? For example :

var a = 1
console.log(a) //console from chrome should display '1'
a = 2 
console.log(a) //console from chrome should now display '2'

But actually it displays :

'2'
'2'

By the way my JS script use used in a HTML file, and that's this HTML file that I'm opening with chrome.

EDIT Sorry, I wanted to simplify the problem so I used an exmample with this simple integer variable, but actually I have this problem with a 2 dimensional array. Here's my code :

var tab = new Array(8).fill(0)
for (x=0;x<8;x++) {tab[x] = new Array(8).fill(0)}
console.log(tab)
tab[0][0] = 2
console.log(tab)

And now with this type of variable (a 2 dimensional array, a 1 dimensional one seems to work fine) I have the same display for both 'console.log' instructions, which is a 8x8 array with a 2 on the top right corner

ArrowRise
  • 608
  • 2
  • 7
  • 1
    [I cannot reproduce it](https://i.imgur.com/PttIoEd.png) + [demo here](https://jsbin.com/zeyurepequ/edit?js,console) – VLAZ Mar 06 '21 at 17:37
  • Also worth noting that you assign numbers to `a` but what is printed is a *string*. – VLAZ Mar 06 '21 at 17:40
  • I think I can't reproduce your problem, but it's something related to manipulating data by reference – Ahmed I. Elsayed Mar 06 '21 at 17:52
  • [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440) | [Weird behavior with objects & console.log](https://stackoverflow.com/q/23429203) – VLAZ Mar 06 '21 at 17:54

0 Answers0