Home >Web Front-end >JS Tutorial >The difference between alert() and console.log() in javascript_javascript skills
[1]alert()
[1.1] It has a blocking effect. If you don’t click OK, subsequent code cannot continue to execute
[1.2] alert() can only output string. If the alert output is an object, the toString() method will be automatically called
e.g. alert([1,2,3]);//'1,2,3'
[1.3] alert does not support the writing of multiple parameters, and can only output the first value
e.g. alert(1,2,3);//1
[2]console.log()
[2.1] Output on the print station
[2.2] Can print any type of data
e.g. console.log([1,2,3]);//[1,2,3]
[2.3] Supports writing of multiple parameters
e.g. console.log(1,2,3)// 1 2 3
The results of alert and console.log are different?
score = [1,2,3]; sortedScore = []; console.log(score); sortedScore = score.sort(sortNumber) console.log(sortedScore); function sortNumber(a, b) { return b - a; }
The above output:
[3, 2, 1]
[3, 2, 1]
But change it to alert:
score = [1,2,3]; sortedScore = []; alert(score); sortedScore = score.sort(sortNumber) alert(sortedScore); function sortNumber(a, b) { return b - a; }
The above output:
1, 2, 3
3, 2, 1
Why is this happening? It shouldn’t all be:
1, 2, 3
3, 2, 1
?
After some research, it was found that it was a problem with chrome implementation. We made inappropriate optimizations for the output and postponed the actual execution of console.log, which is equivalent to "lazy" evaluation when encountering reference types such as arrays and objects. The above problem arises.
https://bugs.webkit.org/show_bug.cgi?id=35801
This is a very historical BUG, which was fixed in the development version last month.