格式化打印
符号 | 描述 |
---|---|
%o |
打印javascript对象,可以是整数、字符串以及JSON数据 |
%d 或%i
|
打印整数 |
%s |
打印字符串 |
%f |
打印浮点数 |
当要替换的参数类型和预期的打印类型不同时,参数会被转换成预期的打印类型。
for (var i=0; i<5; i++) {
console.log("Hello, %s. You've called me %d times.", "Bob", i+1);
}
console.log("I want to print a number:%d","string")
// 输出
// Hello, Bob. You've called me 1 times.
// Hello, Bob. You've called me 2 times.
// Hello, Bob. You've called me 3 times.
// Hello, Bob. You've called me 4 times.
// Hello, Bob. You've called me 5 times.
// I want to print a number:NaN
我们发现string
字符串被转换成数字失败成转换成 NaN
。
console定义样式
%c
为打印内容定义样式
console.log("%cMy stylish message",
"color: red; font-style: italic");