For debugging JavaScript programs, using Console.log () is a better approach than alert () because the alert () function blocks the execution of JavaScript programs, causing side effects; Console.log () The information is printed only in the console, so it does not cause similar concerns.
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, and the viewing methods in the individual browsers can be found in the following pages:
Http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it
Console.log () behavior in different browsers may be different, this article mainly discusses the use of Console.log () in Firebug.
Compatible browsers that do not have 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.
Using 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."
Use other log levels
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: