Tutorials Tonight's Logo
Follow Us

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.

window.console.log("Hello World!"); // with window
console.log(123); // without window since it is global
console.log(10*5);
console.log(Math.PI);
▶ 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, ...)
console.log(123);//number
console.log("Hello World!");//string
console.log(10 + 20);//expression
console.log(new Date());//object
▶ 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, ...)
console.assert(false, "Statement is false");
console.assert(10 > 20, "10 is not greater than 20", { "SomeObject": 12345 });
▶ Try It
javascript console assert

3. console clear

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

console.log(10);
console.log("Hello World!");
console.clear();
console.log("All above logs cleared");
▶ 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".

for (let i = 0; i < 10; i++) {
  console.count("Count number");
  console.log(i);
}
▶ 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?

const arr = [1,2,3,4,5];
console.dir(arr);
console.dir(document.location);
▶ 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().

let num1 = 10, num2 = 0;
if (num2 !== 0) {
  console.log(num1 / num2);
}
else {
  console.error("divided by 0");
}
▶ 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.

const arr = ["a", "b", "c", "d", "e"];
console.table(arr);
const user = {
  name: "Henry",
  id: "iuA98",
  age: 22
}
console.table(user);
▶ 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.

console.time("trackLoop");
for (let i = 0; i < 10000; i++) { }
console.timeEnd("trackLoop");
▶ Try It
Javascript console time

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.


Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.