The Console is mainly used to log the information, warnings and errors associated with a web page. It can be accessed from any global object. Since it is a global object we can use it as
window.console or as a
console object has multiple methods which make it easier to log the information, errors, warnings, etc. Among all
console methods, the
log methods are most commonly used which is used for general-purpose logging.
Here is list of useful
1. console log
console.log(msg1, msg2, ..., obj1, obj2, ...)
2. console assert
assert() console method writes the error message to the console if the assertion (claim) is
false. If the assertion is
true then nothing happens.
console.assert(assertion, message, obj1, obj2, ...)
3. console clear
console.clear() method clears the console if it is allowed by the environment.
4. console count
console.count() method logs the number of time
count() method has been called.
count() method accepts an argument which is a label to the output, it is logged every time
count() function is called. It is optional, it's default value is "default".
5. console dir
console.dir() method displays an interactive list of all the properties of a specified object.
It is presented as a hierarchical listing of methods and properties of an object with closure triangles. Simply
6. console error
error() method of console object is used to output the error in the console.
You can output error message or some object on error using
7. console table
console.table() is used to display data in form of a table. The function takes 1 necessary argument data, which must be an array or an object.
8. console time
console.time() can be used to track how long does an operation take to execute.
console.timeEnd() with the same name then the browser will output the time since timer with the same name started.