The Console Object in JavaScript Explained

In this article, you will learn about The Console Object, its methods. As javascript developers, we all use console.log() methods but there are other methods that we can use while debugging. so let’s get started.

Console Object Methods

MethodDescription
assert()It will write an error message if condition is false
clear()It clears the console
count()It logs the number of time the particular call to count
error()It logs an error message to the console
group()It creates a new inline group in the console.
groupCollapsed()It creates a new inline group in the console.
groupEnd()It exits the current inline group in the console
info()It logs the informational message to the console
log()It logs a message to the console
table()It logs the tabular data as a table to the console
time()It starts a timer (can track how long an operation takes)
timeEnd()It stops a timer that was previously started by console.time()
trace()It logs a stack trace to the console
warn()It logs a warning message to the console

console.assert()

console.assert() method write assertions on the console only if the condition is false. Like some time we need to log things only when the value is false or condition fail

console.assert(condition,message)
console.assert(16%2 === 1,'Sorry 16%2 is 0')

output::
Assertion failed: Sorry 16%2 is 0

console.clear()

console.clear() method clear the console outputs/logs

console.clear()

console.count()

console.count method writes how many time the particular console.count() called. We can use user-defined labels to find the count.

for(let i=0;i<10;i++){
    console.count("loop lable")
}

output::
loop lable: 1
loop lable: 2
loop lable: 3
loop lable: 4
loop lable: 5
loop lable: 6
loop lable: 7
loop lable: 8
loop lable: 9
loop lable: 10

console.error()

console.error() write and error message on the console. It used for colorful representation of error messages

console.error('ERROR')

console.group() & console.groupEnd()

console.group() creates a custom group in the log messages. and console.groupEnd() ends the group

console.log('without group')
console.group('my custom group')
console.log('with group 1')
console.log('with group 2')
console.log('with group 3')
console.log('with group 4')
console.groupEnd()

console.log('without group 1')
console.log('without group 2')

output::
without group
my custom group
  with group 1
  with group 2
  with group 3
  with group 4
without group 1
without group 2

console.info()

console.info() writes informational log to the console

console.info('informational log') 

console.log()

console.info() writes a message to the console

console.log('informational log') 

console.table()

console.table() logs tabular data to the console

const transactions = [{
    id: "7cb1-e041b126-f3b8",
    seller: "WAL0412",
    buyer: "WAL3023",
    price: 203450,
    time: 1539688433
  },
  {
    id: "1d4c-31f8f14b-1571",
    seller: "WAL0452",
    buyer: "WAL3023",
    price: 348299,
    time: 1539688433
  },
  {
    id: "b12c-b3adf58f-809f",
    seller: "WAL0012",
    buyer: "WAL2025",
    price: 59240,
    time: 1539688433
  }];

  console.table(transactions)

output::

(index) │          id          │  seller   │   buyer   │ price  │    time    │
├─────────┼──────────────────────┼───────────┼───────────┼────────┼────────────┤
│    0    │ '7cb1-e041b126-f3b8' │ 'WAL0412' │ 'WAL3023' │ 203450 │ 1539688433 │
│    1    │ '1d4c-31f8f14b-1571' │ 'WAL0452' │ 'WAL3023' │ 348299 │ 1539688433 │
│    2    │ 'b12c-b3adf58f-809f' │ 'WAL0012' │ 'WAL2025' │ 59240  │ 1539688433

console.time() & console.timeEnd()

console.time() start timer on console and console.timeEnd() ends it. these methods are used to messauge time of an operation.

console.time()
for(let i=0;i<10000;i++){
    //come operation
}
console.timeEnd()

output::
default: 0.218ms

console.trace()

console.trace() writes the stack track.

function doSomething(){
    callSomeOne()
}

function callSomeOne(){
    sayGoodBye()
}

function sayGoodBye(){
    console.trace()
}

doSomething()

output::
Trace
    at sayGoodBye (/Users/mac1/Desktop/temp.js:10:13)
    at callSomeOne (/Users/mac1/Desktop/temp.js:6:5)
    at doSomething (/Users/mac1/Desktop/temp.js:2:5)
    at Object.<anonymous> (/Users/mac1/Desktop/temp.js:13:1)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11

console.warn()

console.warn() write warning logs on console

console.warn('WARNING: some warning message')