Consoles in JavaScript
Tags: javascript, webdev, beginners, programming
Published: 2022-09-04 09:30:02

Hello fellow knowledge hungry developers.
console.log() is one of the best friend in a web developers' life, it simply make our lives way easier. But did you know there's more to console than just console.log()? In this post, we are going to discuss about Consoles in JavaScript.

## Introduction
A web console is a tool which is mainly used to log information associated with a web page like: network requests, JavaScript security errors, warning, CSS etc. It enables us to interact with a web page by executing a JavaScript expression in the contents of the page.
### Types of Console
* console.log()
* console.error()
* console.warn()
* console.clear()
* console.time()
* console.table()
* console.count()
* console.group()

## 1. console.log()
Mainly used to log/print the output to the console. We can put any type inside the log(), be it strong, array, object, boolean etc.
```javascript
//console.log() method
console.log('string')
console.log(800)
console.log(true)
console.log(null)
console.log(undefined)
console.log({a:1, b:2}) // object inside log()
console.log([1,2,3,4]) // array inside log()
```

![console.log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eshvv74sax5hl37writm.png)

## 2. console.error()
This method is used to log error messages to the console. IUseful in testing of code. By default, the error message will be highlighted in red color.
```javascript
// console.error() method
console.error('This is a sample Error')
```

![error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ernn931ldj4x8dhygdcb.png)

## 3. console.warn()
Used to log warning messages to the console. By default, the warning message will be highlighted with yellow color.
```javascript
// console.warn() method
console.warn('This is a sample Warning')
```

![warn](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f87lmed5tlyxx6x3w45r.png)

## 4. console.clear()
Used to clear the console. This console will be cleared, incase Chromium based browsers, a simple overlayed text will be printed like one shown in the screenshot below stating _'Console was cleared'_, while in Firefox, no message is returned.
```javascript
// console.clear() method
console.clear()
```

![clear](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22po0qytgy081fkb9yo4.png)

## 5. console.time() and console.timeEnd()
Whenever we want to know the amount of time spent by a block of code, or a function, we can make use ot the time() & timeEnd() methods provided by the JavaScript console object. They take a label which mst be same, and the code inside can be anything(a function, object, simple console.log())
```javascript
// console.time() and console.timeEnd() method
// console.time() method
console.time("Let's see time console")
let time1 = function(){
    console.log('time1 function is running')
}
let time2 = function(){
    console.log('time2 function is running')
}
time1()
time2()
// console.timeEnd() method
console.timeEnd('Time Taken')
```

![time & timeEnd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbxuibsxe2rz3arxldxe.png)

## 6. console.table()
This method allows us to generate a table inside a console. The input must be an array or an object which will be shown as a table.
```javascript
// console.table() method
console.table({a:1, b:2, c:3, d:4, e:5})
```

![table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hi0zdlomkzfhal8de9bp.png)

## 7. console.count()
This method allows us to generate a table inside a console. The input **MUST** be an array or object which will be shows as a table.
```javascript
// console.count() method
console.log('This is a sample count')
for(let i=0; i<10; i++){
    console.count('This is iteration number', i)
}
```

![count](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0b6ijmznj86iz3pi87l8.png)

## 8. console.group() and console.groupEnd()
group() and groupEnd() methods of the console object allows us to group contents in a separate block of code, which will be indented. Just like the time() & timeEnd(), they also accept label, again of the same value.
```javascript
// console.group() and console.groupEnd() method
// console.group() method
console.group('This is a sample group')
console.log('This is a sample group log')
console.warn('This is a sample group warning')
console.error('This is a sample group error')
// console.groupEnd() method
console.groupEnd('This is a sample group')
console.log('This is a new section')
```

![group & gorupEnd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d561sjl9kchyxk7rhc75.png)

**That's all folks 💗**