JavaScript console prints assigned value of variable before it has been assigned?

后端 未结 8 799
Happy的楠姐
Happy的楠姐 2020-12-06 17:47

I\'m deeply confused by the behaviour of either JavaScript, or the Chrome console. Can someone help me understand?

Basically I have the following JavaScript code, no

8条回答
  •  盖世英雄少女心
    2020-12-06 18:03

    Since arrays are passed by reference, every change you make to it will change what is output in the console. It is partly the behavior of Chrome's console, partly JavaScript's.

    If you want to print the result at the time of the call to console.log, you could output it as a string using JSON.stringify.

    console.log("COPIED 1", JSON.stringify(copied_array));
    

    Important edit

    It seems I was mostly wrong. As diEcho pointed out in the question's comments, a similar question has a better answer. It seems to be solely Chrome behavior.

提交回复
热议问题