Home > Web Front-end > JS Tutorial > body text

The difference between alert() and console.log() in javascript_javascript skills

WBOY
Release: 2016-05-16 15:42:47
Original
1300 people have browsed it

[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;
}

Copy after login

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;
}
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template