显示信息
1 2 3 4
| console.log('log'); //最常用 console.info('info'); console.error('error'); console.warn('warn');
|
效果:

占位符
console
上述的集中度支持printf
的占位符格式,支持的占位符有:字符(%s)、整数(%d或%i)、浮点数(%f)和对象(%o)
1
| console.log("%d年%d月%d日",2016,6,1);
|
效果:

信息分组
1 2 3 4 5 6 7 8
| console.group("第一组信息"); console.log("第一组第一条:我的主页(http://h5demo.xyz)"); console.log("第一组第二条:我的博客(http://blog.h5demo.xyz)"); console.groupEnd(); console.group("第二组信息"); console.log("第二组第一条:Leo Angel"); console.log("第二组第二条:前端工作者"); console.groupEnd();
|
效果:

查看对象的信息
console.dir()
可以显示一个对象所有的属性和方法
1 2 3 4 5 6
| var info = { index: "http://h5demo.xyz", blog: "http://blog.h5demo.xyz", info: "前端爱好者欢迎你的加入" }; console.dir(info);
|
效果:

显示某个节点的内容
console.dirxml()
用来显示网页的某个节点所包含的html/xml
代码
1 2
| var info = document.getElementById("info"); console.dirxml(info)
|
效果:

判断变量是否是真
console.assert()
用来判断一个表达式或变量是否为真。如果结果为否,则在控制台输出一条相应信息,并且抛出一个异常
1 2 3 4
| var result = 1; console.assert(result); var year = 2016; console.assert(year == 2018);
|
1是非0值,是真;而第二个判断是假,在控制台显示错误信息
效果:

追踪函数的调用轨迹
console.trace()
用来追踪函数的调用轨迹
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function add(a, b) { console.trace(); return a + b; } var x = add3(1, 1); function add3(a, b) { return add2(a, b); } function add2(a, b) { return add1(a, b); } function add1(a, b) { return add(a, b); }
|
效果:

计时功能
console.time()
和console.timeEnd()
,用来显示代码的运行时间
1 2 3 4 5
| console.time("计时器一"); for (var i = 0; i < 1000; i++) { for (var j = 0; j < 1000; j++) {} } console.timeEnd("计时器一");
|
运行时间是8.745ms
效果:

console.profile()的性能分析
性能分析就是分析程序各个部分的运行时间,找出瓶颈所在,使用的方法是console.profile()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function All() { alert(11); for (var i = 0; i < 10; i++) { funcA(1000); } funcB(10000); } function funcA(count) { for (var i = 0; i < count; i++) {} } function funcB(count) { for (var i = 0; i < count; i++) {} } console.profile('性能分析器'); All(); console.profileEnd();
|
效果:

CSS样式
具体来说,是可以对输出到console
控制台的文字进行CSS
控制。
格式如下:
1
| console.log("%c需要输出的信息 ", "css 代码");
|
3D 文字

渐变背景

彩虹文字
