JavaScript Debugging Techniques Console.log () detailed _javascript skills

Source: Internet
Author: User

One, what is Console.log ()?
In addition to some very old versions of browsers, most browsers now have their own debugging capabilities, and can be supplemented by installing plug-ins even without debugging. For example, the old version of Firefox does not have its own debugging tools, in which case you can install the Firebug plug-in to add debugging capabilities. On a browser that has debugging capabilities, a member variable named console is registered in the Window object, referring to the console in the Debug tool. By calling the log () function of the console object, you can print the information in the console. For example, the following code will print "Sample log" in the console:

Copy Code code as follows:
Window.console.log ("Sample log");

The preceding code can omit the window object and directly abbreviate:
Copy Code code as follows:
Console.log ("Sample log");

Console.log () can accept any string, numeric, and JavaScript objects. Like the alert () function, Console.log () can also accept line breaks and tab \ t. The debug information printed by the Console.log () statement can be seen in the browser's debug console. Console.log () behavior in different browsers may be different, this article mainly discusses the use of Console.log () in Firebug.
compatible browsers without a debug console
The Console object in window does not exist for older browsers that are missing the debug console, so using the Console.log () statement directly may cause errors inside the browser (null pointer error) and eventually cause the crash of some old versions of the browser. To solve this problem, you can define the console object and declare that the log function of the console object is an empty function, so that when the Console.log () statement executes, the older browsers will not do anything:
Copy Code code as follows:
if (!window.console) {
Window.console = {log:function () {}};
}

However, in most cases, there is no need to do this compatibility work-console.log () and other debugging code should be removed from the final product code.
third, the use of parameters
Like the alert () function, Console.log () can also accept a variable and concatenate it with another string:
Copy Code code as follows:
Use variable
var name = "Bob";
Console.log ("The name is:" + name);

Unlike the alert () function, Console.log () can also accept a variable as a parameter passed into a string, whose specific syntax is consistent with the printf syntax in the C language:
Copy Code code as follows:
Use parameter
var people = "Alex";
var years = 42;
Console.log ("%s is%d years old.", people, years);

The execution of the above code results in the following: "Alex is years old."
Four, use other log level
In addition to Console.log (), Firebug also supports several different logging levels: Debug, info, warn, and error. The following code prints these different log levels of information in the console:
Copy Code code as follows:
Use different logging level
Console.log ("log Level");
Console.debug ("debug level");
Console.info ("info level");
Console.warn ("Warn level");
Console.error ("error level");

You can see from the Firebug console that print information at different log levels is not the same color as the icon, and you can select different log levels in the console to filter this information:

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.