格式化打印
| 符號(hào) | 描述 |
|---|---|
%o |
打印javascript對象,可以是整數(shù)、字符串以及JSON數(shù)據(jù) |
%d或%i
|
打印整數(shù) |
%s |
打印字符串 |
%f |
打印浮點(diǎn)數(shù) |
當(dāng)要替換的參數(shù)類型和預(yù)期的打印類型不同時(shí),參數(shù)會(huì)被轉(zhuǎn)換成預(yù)期的打印類型。
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
我們發(fā)現(xiàn)string字符串被轉(zhuǎn)換成數(shù)字失敗成轉(zhuǎn)換成 NaN。
console定義樣式
%c為打印內(nèi)容定義樣式
console.log("%cMy stylish message",
"color: red; font-style: italic");

定義樣式