console.log() shows the changed value of a variable before the value actually changes

后端 未结 7 1993
无人共我
无人共我 2020-11-22 02:09

This bit of code I understand. We make a copy of A and call it C. When A is changed C stays the same

var A = 1;
var C = A;
console.log(C); // 1
A++;
console.         


        
7条回答
  •  渐次进展
    2020-11-22 02:39

    EDIT: Keeping this answer just to preserve useful comments below.

    @Esailija is actually right - console.log() will not necessarily log the value the variable had at the time you tried to log it. In your case, both calls to console.log() will log the value of C after sorting.

    If you try and execute the code in question as 5 separate statements in the console, you will see the result you expected (first, [2, 1], then [1, 2]).

提交回复
热议问题