See: Console API on MDN and their results in Chrome.
console.assert(1 === true, "assert 1 is strictly equal to true");
-> Assertion failed: assert 1 is strictly equal to true
console.log({a: "eh", b: "bee"}, [1,2,4,8,16]);
-> Object {a: "eh", b: "bee"} [1, 2, 4, 8, 16]
Also supports string-substitution:
console.log("Hello, %s. You've called me %d times.", "Barb", 200.9);
-> Hello, Barb. You've called me 200 times.
Some browsers alias this as console.debug(…).
console.dir({a: "eh", b: "bee"}, [1,2,4,8,16]);
-> ▶ Object
Notice only the first argument is used.
"Object" Expandable/inspectable.
-> ▼ Object
a: "eh"
b: "bee"
Chrome doesn't implement this — same result as a console.log.
(function willTrace() {
console.trace({a: "eh", b: "bee"}, [1,2,4,8,16]);
})();
-> (x) ▶ Object {a: "eh", b: "bee"} [1, 2, 4, 8, 16]
Expands with stack-trace/context:
-> ▼ Object {a: "eh", b: "bee"} [1, 2, 4, 8, 16]
willTrace @ console.html:44
(anonymous function) @ console.html:45
Error is basically the same with a red scheme and an (x)
to the left.
(function willError() {
console.error({a: "eh", b: "bee"}, [1,2,4,8,16]);
})();
-> (x) ▶ Object {a: "eh", b: "bee"} [1, 2, 4, 8, 16]
Expands with stack-trace/context of error:
-> (x) ▼ Object {a: "eh", b: "bee"} [1, 2, 4, 8, 16]
willError @ console.html:15
(anonymous function) @ console.html:16
In some browsers console._exception() is an alias for error (not chrome though, which throws an error: ha!)
console.group();
console.log("thing-a");
console.log("thing-b");
console.log("thing-c");
console.groupEnd();
-> ▼
| thing-a
| thing-b
| thing-c
Alternatively you can use groupCollapsed to achieve the same result, in a pre-collapsed state:
console.groupCollapsed();
console.log("thing-a");
console.log("thing-b");
console.log("thing-c");
console.groupEnd();
-> ▶
log and info are nearly the same except info has a blue info symbol to it's left:
console.log({what: "my log"});
-> {what: "my log"}
console.info({what: "my info"});
-> (i) {what: "my info"}
warn is the same as log/info except it has a yellow background and a yield-sign to it's left:
console.warn({what: "my warn"});
-> (^) Object {what: "my warn"}
really neat, implements an internal count of the number of times a line is run (in blue text):
function countThis() {
console.count("This line run");
}
countThis();
countThis();
countThis();
-> This line run: 1
This line run: 2
This line run: 3
Nicely formats any double nested data like:
array containg arrays
console.table([
['a', 'b'],
['c', 'd', 'e'],
['f', 'g'],
['h']
]);
array containing objects
console.table([
{base: 2, exponent: 0, result: 1},
{base: 2, exponent: 1, result: 2},
{base: 2, exponent: 2, result: 4},
{base: 2, exponent: 3, result: 8}
]);
object containing arrays
console.table({
two: [1, 2, 4, 8],
three: [1, 3, 12, 27, 41],
four: [1, 4, 16, 64, 256, 1024],
five: [1, 5, 25, 125, 625, 3125]
});
object containing objects
console.table({
monday: {place: "flyods", dish: "breakfast burrito"},
wednesday: {place: "gravy", dish: "b + g"},
friday: {place: "tasty 'n alder", dish: "cobowy breakfast"},
});
At the third level (object -> array -> object, array -> array -> array, etc…) just shows a string
console.table([
['1a', '1b', ['1c', 67, 78, 89]],
['2a', {a: 'eh', b: 'bee'}, '2c']
]);
-> | (index) | 0 | 1 | 2 |
| --- | --- | --- | --- |
| 0 | "1a" | "1b" | Array[4] |
| 1 | "2a" | Object | "2c" |
A single level value (just an array or object) doesn't show a table but simply what console.dir(…) already shows.
A string is required to id the timer:
console.time("timer1");
console.timeEnd("timer1");
-> timer1: 0.003ms
Non-strings are simply coerced:
console.time(1);
console.timeEnd(1);
-> 1: 0.003ms
console.time({});
console.timeEnd({a: 1});
-> [object Object]: 0.037ms
console.time([]);
console.timeEnd([]);
-> : 0.022ms
console.time('');
console.timeEnd([]);
-> : 0.022ms
No-arg not gonna cut it:
console.time();
console.timeEnd();
-> undefined
Also notice you cannot restart an active timer:
console.time("no restart");
setTimeout(function() {
console.time("no restart"); // has no effect
console.timeEnd("no restart");
}, 999);
-> "no restart": 1002.231ms
function runSum(start, stop) {
for (sum = 0; start < stop; start++)
sum += start;
return sum;
}
function profile(key, profileThis) {
console.profile(key);
var result = profileThis();
console.profileEnd(key);
console.info(key + ": " + result);
}
for (var i = 0, len = 9; i < len; i++) {
var to = Math.pow(10, i);
profile(
'runSum(1, 10^' + i + ')',
function() { return runSum(1, to); }
);
}