Differences between alert () and console. log () in javascript _ javascript tips-js tutorial

Source: Internet
Author: User
When debugging js, we can use alert to display information and Debug programs. The alert pop-up window will interrupt the program. If you want to display information in a loop, you will be exhausted by clicking close window. In addition, the alert Display object is always displayed as [object]. Although the self-written log can display some object information, many functions do not support the console. [1] alert ()

[1.1] blocking effect. If you do not click OK, subsequent Code cannot be executed.

[1.2] alert () can only output strings. If alert outputs an object, the toString () method is automatically called.

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

[1.3] alert does not support 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 printer

[2.2] printing any type of data

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

[2.3] Support for writing multiple parameters

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

What are the results of alert and console. log?

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 changed 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? Not all:
1, 2, 3
3, 2, 1
?

After some research, it is found that the problem of chrome implementation is that the output is not properly optimized, and the console is used. the actual execution of log is postponed, which is equivalent to the value of "inertia". In case of reference types such as arrays and objects, the above problem occurs.

Https://bugs.webkit.org/show_bug.cgi? Id = 35801

This is a very historical BUG. It was fixed in the development version last month.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.