Introduction to the console. assert () function in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the console in JavaScript. introduction to The assert () function. The assert () function is an asserted tool function that is frequently used in Debugging. if you need it, refer to the development and maintenance process of JavaScript programs, assert is a good feature for ensuring program correctness. In browsers with debugging tools, this feature can be achieved by calling console. assert. For example, in the following code, the console. assert () statement ensures that the score variable length of the cat object is 3:

The code is as follows:


Function cat (name, age, score ){
This. name = name;
This. age = age;
This. score = score;
}
Var c = new cat ("miao", 2, [6, 8, 7]);
Console. assert (c. score. length = 3, "Assertion of score length failed ");


In the console. in The assert () statement, the first parameter is the result of assert, which should be true under normal conditions. The second parameter is the error message printed on the console when an error occurs. For example, if the length of the score variable array is not 3 in the preceding example:

The code is as follows:


Function cat (name, age, score ){
This. name = name;
This. age = age;
This. score = score;
}
Var c = new cat ("miao", 2, [6, 8]);
Console. assert (c. score. length = 3, "Assertion of score length failed ");


After the code is executed, the Firebug console prints the error message:

Browser support

Console. assert () provides better support for browsers with debugging tools. all major browsers support this function. However, Firefox does not support this function. you must install the Firebug plug-in on Firefox to use console. assert ().

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.