Declare an empty array in js, print it first, and then push a value. Why is there a value when printing?

let a = []
console.log (a)
a.push (1)

the console shows an empty array,
clipboard.png


clipboard.png

Apr.03,2021

because Array is reference type in js, although you console.log > before push , because reference type is address is not pass value , the array you see in the console will be updated with the subsequent operation of the array


So you can see the value of push after that. In other words, console.log is just a snapshot of that time, so it comes out as an empty array, but it still maintains a connection with memory. When you click expand, the browser reads the value in it from memory in real time. I don't know if you can understand that.


this example fully shows that console.log is asynchronous , so try not to use console to debug your app.

.

the empty one above is just a snapshot, and the real data is when you click on it.

if you change a to the basic type, you will never have this problem.


the root cause is because it is a reference type wow

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e79687-4652e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e79687-4652e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?