Home  >  Article  >  Web Front-end  >  Use the console object of Js to print debugging information on the console to test the implementation of Js

Use the console object of Js to print debugging information on the console to test the implementation of Js

高洛峰
高洛峰Original
2016-12-05 10:49:581376browse

By chance, I pressed F12 when opening Baidu, and then I saw Baidu’s recruitment advertisement output in the console. It felt very cool, and then I came up with this blog post.

Since you can output information on the console like this, wouldn’t it save you a lot of trouble when debugging Js in the future? To avoid misleading others, I specifically used for(var i in console) to check the console support of various browser consoles. The results are as follows:

IE 11 console

log , info , warn , error , debug , assert , time , timeEnd , group , groupCollapsed , groupEnd , trace , clear , dir , dirxml , count , countReset , cd , select , profile , profileEnd

Firebug Console

log , debug , info , warn , exception , assert , dir , dirxml , trace , group , groupCollapsed , groupEnd , profile , profileEnd , count , clear , time , timeEnd , timeStamp , table , error

Chrome Console

memory , _commandLineAPI , debug , error , info , log , warn , dir , dirxml , table , trace , assert , count , markTimeline , profile , profileEnd , time , timeEnd , timeStamp , timeline , timelineEnd , group , groupCollapsed , groupEnd , clear

It can be seen that the browser I tested above is suitable for The five basic methods of log, info, warn, error, and debug are all supported. Note that I am using IE 11. I have not tested other versions. Firefox itself does not have a console. You need to load the Firebug plug-in and start it. , to console, otherwise Js will report an error. In order to make it more convenient to use, you can encapsulate it yourself and check the browser's support for console. If it does not support it, you can only use the original alert or other methods.

Simple usage:

console.log ("Log information");

console.info ("General information");

console.debug ("Debug information");
console.warn ("Warning prompt");
console.error("Error message");

Formatted output:

console.log("%d year %d month %d day", 2014, 5, 20); // Date format output

console. log('%c colored output information', 'color:white; background-color:#0055CC');//Format output


Output variable:


var who= 'you';

console.log( 'Output variable We support ', you);//Read variable


Output array:


var arr = [1, 2, 3, 4, 5];

console.log('Array: ', arr) ;//Output array


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn