The string in bracket will be printed out after the console.timeEnd() is reached. The string for taking the time is an identifier and has to be in .time() and .timeEnd() the same.
console.time(' ...logging ends after: ');
// doing
// some
// computation
console.timeEnd(' ...logging ends after: ');
console.clear();
Without label:
var user = "";
function greet() {
console.count();
return "hi " + user;
}
user = "bob";
greet();
user = "alice";
greet();
greet();
console.count();
// output:
"<no label>: 1"
"<no label>: 2"
"<no label>: 3"
"<no label>: 1"
With label:
var user = "";
function greet() {
console.count(user);
return "hi " + user;
}
user = "bob";
greet();
user = "alice";
greet();
greet();
console.count("alice");
// output:
"bob: 1"
"alice: 1"
"alice: 2"
"alice: 3"
Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
Syntax:
console.assert(assertion, obj1 [, obj2, ..., objN]);
console.assert(assertion, msg [, subst1, ..., substN]); // c-like message formatting
Parameters
assertion
obj1 ... objN
msg
subst1 ... substN
function greaterThan(a,b) {
console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b});
}
greaterThan(5,6);
// output to console:
Assertion failed: Object {message: "a is not greater than b", a: 5, b: 6}
if(a.childNodes.length < 3 ) {
console.warn('Warning! Too few nodes (%d)', a.childNodes.length);
}
console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large");
console.table([{a:1, b:2, c:3}, {a:"foo", b:false, c:undefined}]);
console.table([[1,2,3], [2,3,4]]);
or more complex:
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
var family = {};
family.mother = new Person("Susan", "Doyle", 32);
family.father = new Person("John", "Doyle", 33);
family.daughter = new Person("Lily", "Doyle", 5);
family.son = new Person("Mike", "Doyle", 8);
console.table(family, ["firstName", "lastName", "age"]);