Console. log () function in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the console in JavaScript. the log () function is described in detail. This article describes what is the console. log (), compatible with browsers that do not debug the console, use parameters, use other log levels, and so on. For more information about debugging JavaScript programs, see alert (), use console. log () is a better method because the alert () function blocks the execution of JavaScript programs and causes side effects. log () only prints relevant information on the console, so it does not cause similar concerns.

What is console. log ()?

In addition to some older browsers, most browsers now have built-in debugging functions. Even if there is no debugging function, you can install plug-ins to supplement it. For example, in earlier versions of Firefox, there is no built-in debugging tool. In this case, you can install the Firebug plug-in to add debugging functions. In a browser with debugging functions, a member variable named console is registered in the window object, which refers to the console in the debugging tool. You can print information on the console by calling the log () function of the console object. For example, the following code prints "Sample log" on the console ":


The Code is as follows:


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


The preceding code can ignore the window object and be abbreviated:


The Code is as follows:


Console. log ("Sample log ");


Console. log () can accept any string, number, and JavaScript Object. Similar to the alert () function, console. log () can also accept line breaks \ n and tabs.

The debugging information printed by the console. log () statement can be viewed on the debugging console of the browser. For details about how to view the information in each browser, refer to the following webpage:
Http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it

The console. log () behavior may vary in different browsers. This article mainly discusses the use of console. log () in Firebug.

Compatible with Browsers without the debugging Console

For browsers of earlier versions that lack the debugging console, the console object in the window does not exist, so you can directly use the console. the log () statement may cause errors (NULL pointer errors) inside the browser, and eventually crash in some older browsers. 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. In this way, when the console. when the log () Statement is executed, these old browsers will not do anything:


The Code is as follows:


If (! Window. console ){
Window. console = {log: function (){}};
}


However, in most cases, there is no need to do this compatibility work-debugging code such as console. log () should be removed from the final product code.

Parameters Used

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


The Code is as follows:


// Use variable
Var name = "Bob ";
Console. log ("The name is:" + name );


Unlike the alert () function, console. log () can also pass variables as parameters to strings. The syntax is the same as that of printf in C:


The Code is as follows:


// Use parameter
Var people = "Alex ";
Var years = 42;
Console. log ("% s is % d years old.", people, years );


The execution result of the above Code is: "Alex is 42 years old ."

Use other log levels

In addition to console. log (), Firebug also supports different log levels: debug, info, warn, and error. The following code prints information at different log levels on the console:


The Code is 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 the color and icon of printed information at different log levels are different. You can also select different log levels in the console to filter the 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.