James Sherry PRO
Web Development Tutor and Co-Founder of { The Jump } Digital School
console.info("This is some info");
console.debug("This is a debug message");
console.warn(
"This is a warning about something happening in your code. It has a stack trace to assist with debugging"
);
console.error(
"This is an error. It has a stack trace to assist with debugging. It is not the same as throwing an error because it does not stop the code."
);
console.log("After the 'error'");
/* getting a stack trace */
function a(f) {
f();
}
function b() {
console.trace();
}
a(b);
/* console.assert - for testing */
let number = 2;
const errorMsg = "The number is not even";
console.assert(number % 2 === 0, { number: number, errorMsg: errorMsg });
number += 1;
console.assert(number % 2 === 0, { number: number, errorMsg: errorMsg });
/* console.table - for display */
const obj = {
subobject: {
property: "value",
other: "value"
},
property: "value"
};
console.table(obj, ["subobject"]);
const people = [
{ firstName: "James", lastName: "Sherry" },
{ firstName: "Robert", lastName: "Sherry" },
{ firstName: "Katie", lastName: "Smith" }
];
console.table(people);
console.table(people, ["firstName"]); // restrict columns
/* Counters */
console.count();
console.count();
console.count();
console.countReset(); // reset
console.count();
// Giving it a name creates a new counter
console.count("James");
console.count("James");
console.count("James");
/* TIMERS */
// Create/start one (with a label/name) (You can have <10k timers)
console.time('some process');
setTimeout(() => {
// get it's current time
console.timeLog('some process');
}, 1000);
setTimeout(() => {
// stop/destroy
console.timeEnd('some process');
}, 2000);
/* GROUPS */
console.log("This is the outer level");
console.group();
console.log("Level 2");
console.group();
console.log("Level 3");
console.warn("More of level 3");
console.groupEnd();
console.log("Back to level 2");
console.groupEnd();
console.log("Back to the outer level");
/* groupCollapsed starts the groups off closed */
console.log("This is the outer level");
console.groupCollapsed();
console.log("Level 2");
console.groupCollapsed();
console.log("Level 3");
console.warn("More of level 3");
console.groupEnd();
console.log("Back to level 2");
console.groupEnd();
console.log("Back to the outer level");
/*****
* CSS IN YOUR LOGS!!
*****/
/*
THere is an old syntax from C called 'string substitution'. It works by you putting typed placeholders into your string and then passing the string to console and then further arguments to be inserted, so:
console.log('My name is %s', 'James');
// The types you can insert are:
%o or %O
Outputs a JavaScript object. Clicking the object name opens more information about it in the inspector.
%d or %i
Outputs an integer. Number formatting is supported, for example console.log("Foo %.2d", 1.1) will output the number as two significant figures with a leading 0: Foo 01
%s
Outputs a string.
%f
Outputs a floating-point value. Formatting is supported, for example console.log("Foo %.2f", 1.1) will output the number to 2 decimal places: Foo 1.10
%c is a directive that allows CSS to be used on the message. You can use:
background and its longhand equivalents.
border and its longhand equivalents
border-radius
box-decoration-break
box-shadow
clear and float
color
cursor
display
font and its longhand equivalents
line-height
margin
outline and its longhand equivalents
padding
text-* properties such as text-transform
white-space
word-spacing and word-break
writing-mode
*/
console.log(
"Multiple styles: %cred %corange",
"color: red",
"color: orange",
"Additional unformatted message"
);
console.log(
"This is %cMy stylish message",
"color: yellow; font-style: italic; background-color: blue;padding: 2px"
);
Note the 'waterfall' on the right:
The play controls:
The play controls:
The Application tab shows the state of things like:
By James Sherry
Via developer tools