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

The difference between alert() and console.log() in javascript

高洛峰
Release: 2017-02-03 14:45:19
Original
1649 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 alert The output is that the object will automatically call the toString() method

e.g. alert([1,2,3]);//'1,2,3'

[1.3]alert is not supported When writing multiple parameters, only the first value can be output

e.g. alert(1,2,3);//1

[2]console.log()

[2.1]Output on the print station

[2.2]You can print any type of data

e.g. console.log([1,2,3]);//[1,2 ,3]

[2.3]Support multiple parameter writing methods

e.g. console.log(1,2,3)// 1 2 3

alert and console. The result of log is 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? Shouldn't they all be:
1, 2, 3
3, 2, 1
?

After some research, I found that it was a problem with chrome implementation. I made inappropriate optimizations for the output and postponed the actual execution of console.log, which is equivalent to "lazy" evaluation. When encountering arrays and objects, this The reference type causes the above problem.

https://bugs.webkit.org/show_bug.cgi?id=35801

This is a very historical BUG, ​​which was fixed in the development version last month .

For more articles related to the difference between alert() and console.log() in javascript, please pay attention to the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!