# console
一般地:
const hello = "Hi there, welcome to MDN Web Docs!";
console.log(hello);
// Hi there, welcome to MDN Web Docs!
const currentDate = new Date();
const formattedDate = new Intl.DateTimeFormat("en-US").format(currentDate);
console.log("Today's date is %s", formattedDate);
// Today's date is: 11/29/2023
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
log分类:
info
、warn
、error
。
console.table()
const dogs = [
{ name: "Yoshi", color: "Black", personality: "Calm" },
{ name: "Melanie", color: "Brown", personality: "Hyperactive" },
{ name: "Peppers", color: "white", personality: "Unpredictable" },
];
console.table(dogs);
const cat = {
a: {b: 'c'}
};
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
console.count()
function postBoostClicked() {
// My post has been boosted, do something here
console.count("Boost count");
}
postBoostClicked();
// Boost count: 1
postBoostClicked();
// Boost count: 2
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
console.time()
console.time("timerName");
// call myFunction()
console.timeEnd("timerName");
// timerName: 200ms - timer ended
1
2
3
4
2
3
4
console.time("MyTimer");
console.timeLog("MyTimer", "Starting application up…");
// MyTimer: 0ms Starting application up…
// call myFunction(), for example
console.timeLog("MyTimer", "UI is setup, making API calls now");
// MyTimer: 200ms UI is setup, making API calls now
// call otherFunction(), for example
console.timeEnd("MyTimer");
// MyTimer: 300ms - timer ended
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
console.group()
console.group("Grouped Logs");
console.log("Log 1");
console.log("Log 2");
console.groupEnd();
// Grouped Logs
// Log 1
// Log 2
console.groupCollapsed("Collapsed Group");
console.log("Log 3");
console.log("Log 4");
console.groupEnd();
// > Collapsed Group
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
console.trace()
// Is the "currentSeconds" value odd or even?
const currentSeconds = new Date().getSeconds();
const condition = currentSeconds % 2 === 0;
function one() {
example();
}
function two() {
example();
}
function randomChoice() {
if (!condition) {
// OK, I'm lost!
one();
} else {
two();
}
}
function example() {
// Where is this function called?
console.trace("Trace from example() function");
}
randomChoice();
// console.trace() Trace from example function
// example debugger eval code:23
// one debugger eval code:6
// randomChoice debugger eval code:16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
console.clear()