tutorialstonight logo
A Platform To Learn To Code

JAVASCRIPT CONSOLE



What Is A Console In Javascript?

Console is an Object in javascript which provide access to browser's debugging console. It is also a web tool.

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.

Example

Try It
Javascript console

Javascript Console Methods

Javascript 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.

Javascript Console methods

Here is list of useful console methods:

  1. console.log()
  2. console.assert()
  3. console.clear()
  4. console.count()
  5. console.dir()
  6. console.error()
  7. console.table()
  8. console.time()

1. console log

console.log() is the most frequently used console method. It is used to output a message in the console, the message can be string, number, array, object and can also evaluate a javascript expression.

console.log(msg1, msg2, ..., obj1, obj2, ...)

Example

Try It

2. console assert

The 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, ...)

Example

Try It
javascript console assert

3. console clear

The console.clear() method clears the console if it is allowed by the environment.

Example

Try It

4. console count

The console.count() method logs the number of time count() method has been called.

The 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".

Example

Try It

5. console dir

The 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 console.dir() let us see all the properties of a specified Javascript object.

What is difference between console.log and console.dir?

Example

Try It
javascript console dir

6. console error

The 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 console.error().

Example

Try It

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.

Example

Try It
Javascript console table

8. console time

console.time() can be used to track how long does an operation take to execute.

To use console.time() you have to give a timer a unique name then execute the javascript codes and when you call console.timeEnd() with the same name then the browser will output the time since timer with the same name started.

Example

Try It
Javascript console time