A console.log () Explanation of JavaScript debugging techniques

Source: Internet
Author: User

Console.log () Usage of javascript

First, what is Console.log ()?

In addition to some very old browsers, most browsers now have debug capabilities, and even without debugging, you can add them by installing plugins. For example, the old version of Firefox does not have its own debugging tools, in which case you can add debugging features by installing the Firebug plugin. On a browser with debugging capabilities, a member variable named console is registered in the Window object, referring to the console in the debugging tool. You can print information in the console by invoking the log () function of the console object. For example, the following code will print "Sample log" in the console:

Copy the Code code as follows:

Window.console.log ("Sample log");


The code above can omit the window object and simply shorthand:

Console.log ("Sample log");

Console.log () can accept any string, number, and JavaScript object. Similar to the alert () function, Console.log () can also accept newline characters \ n and tab \ T. The debug information printed by the Console.log () statement can be seen in the debug console of the browser. Console.log () behavior may vary in different browsers, this article focuses on the use of Console.log () in Firebug.


Second, compatible browser without debug console

For older browsers that are missing the debug console, the Console object in window does not exist, so using the Console.log () statement directly may cause errors inside the browser (null pointer errors) and eventually cause some older browsers to crash. To solve this problem, you can manually 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, these older browsers will not do anything:

if (!window.console) {window.console = {log:function () {}};}


However, in most cases, it is not necessary to do this compatibility work-console.log () and other debugging code should be removed from the final product code.


Third, the use of parameters

Similar to the alert () function, Console.log () can also accept variables and splice them with other strings:

Use variablevar name = "Bob", Console.log ("The name is:" + name);


Unlike the alert () function, Console.log () can also accept variables passed as arguments to a string, with specific syntax consistent with the printf syntax in the C language:

Use Parametervar people = "Alex"; var years = 42;console.log ("%s was%d years old.", people, years);


The above code executes as follows: "Alex is-years old."


Iv. use of other log levels
In addition to Console.log (), Firebug also supports a number of different log levels: Debug, info, warn, error. The following code prints information for these different log levels in the console:

Use different logging Levelconsole.log ("Log Level"), Console.debug ("Debug Level"), Console.info ("info level"); Console.warn ("Warn Level"), Console.error ("error level");


From the Firebug console, you can see that the color and icon of the different log levels are not the same, and that you can filter the information by selecting different log levels in the console:



A console.log () Explanation of JavaScript debugging techniques

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.