JavaScript-30-Day-9
Tags: javascript, productivity, webdev, github
Published: 2021-06-09 17:24:19

##<center> Developer Tools and Console Tricks </center>

[demo](https://cenacrharsh.github.io/JS-30-DAY-9/)

![ss](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6l6qgrt6ghjligw47z29.png)

On Day-9 of JavaScript-30 we learned a lot of handy console tricks and dev tools to increase our productivity in Web Development.

##Lessons Learned:

-Suppose we want to know what is causing a particular thing to happen on a site, you know it's javascript making it happen but not sure exactly which line is responsible.

There is a handy dev tool for us:

Just open the console -> find the element in DOM on which the change is happening -> right click on it -> go to **break on** -> select **attribute modifications**

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

Next time when you click on the element, it's going to pop a debugger command in. It's a break point for us and it will pause exactly at the line of code that is causing the attribute.
(remove the break point from the same place).

##Now we'll see some handy console tricks:

##Regular
```javascript
console.log("Hello");
```

This is our regular `console.log()` which simply outputs on the console.

![regular](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n7hf01fifs4u47397zl.png)

##Interpolated
```javascript
console.log("Hello I am a %s string", "💩");
```

![interpolation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wkecple5rvqi9zxefj2.png)

It interpolates whatever we have passed into the second one into our first message.

##Styled
```javascript
console.log("%c I am some great text", "font-size:20px;background:red;");
```

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

We can style our text with `%c` and apply any font-css.

##warning!
```javascript
console.warn("OH NOOOO!");
```

![warning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dd5srzqni9o5ykmzbk9.png)

It displays a warning on the console as well as the stack trace as to where the warning came from.

##Error:
```javascript
console.error("Oh Shit!");
```

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

It displays a error message in the console along with the stack trace as to where it was generated.

##Info
```javascript
console.info("Crocodiles eat 3-4 people per year");
```

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

> In firefox it displays with a small 'i' icon.

##Testing
```javascript
console.assert(1 === 2, "That is Wrong!");
```

![assert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7aqsrnrclm0wgcpc59xm.png)

Assert is fired only if something is false. So using assert we can test for something and if it is false it will throw an error.

##clearing
```javascript
console.clear();
```

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

It clears everything on the console.

##Viewing DOM Elements
```javascript
const p = document.querySelector("p");
console.log(p);
```
Using `console.log(p)` actual element is displayed.

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

```javascript
console.dir(p);
```
But using `console.dir(p)` displays all the properties and methods  associated with that element.

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

##Grouping together
```javascript
const dogs = [
  { name: "Snickers", age: 2 },
  { name: "hugo", age: 8 },
];

dogs.forEach((dog) => {
  console.groupCollapsed(`${dog.name}`);
  console.log(`This is ${dog.name}`);
  console.log(`${dog.name} is ${dog.age} years old`);
  console.log(`${dog.name} is ${dog.age * 7} dog years old`);
});
```
This is how the normal output is like:
![without collapse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7p6ao9ycn9omt95k1ic.png)

Now using grouping
```javascript
dogs.forEach((dog) => {
  //console.group(`${dog.name}`);
  console.groupCollapsed(`${dog.name}`);
  console.log(`This is ${dog.name}`);
  console.log(`${dog.name} is ${dog.age} years old`);
  console.log(`${dog.name} is ${dog.age * 7} dog years old`);
  console.groupEnd(`${dog.name}`);
});
```
>We can either use `console.group()` or `console.groupCollapsed()`, the collapsed displays output where the output groups are collapsed by default.

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

##counting
```javascript
console.count("Harsh");
console.count("Harsh");
console.count("Harsh");
console.count("Harsh");
console.count("Harsh");
```
![counts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s846u42ezk4ikna2jkb9.png)

It counts how many times we have used a specific word, or number, or object , or DOM node etc.

##timing
```javascript
console.time("fetching data");
fetch("https://api.github.com/users/cenacrharsh")
  .then((data) => data.json())
  .then((data) => {
    console.timeEnd("fetching data");
    console.log(data);
  });
```

!timing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3tz6gy97yiy1rmtcnq4.png)

If we want to track how long a particular task takes we can start a timer.

##table
```javascript
console.table(dogs);
```
It displays an array object in the form of a table.

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

and with this our project for the day was completed.

GitHub repo:
{% github cenacrharsh/JS-30-DAY-9 no-readme %}

###Blog on Day-8 of javascript30
{% link https://dev.to/cenacr007_harsh/javascript-30-day-8-4lhj %}

###Blog on Day-7 of javascript30
{% link https://dev.to/cenacr007_harsh/javascript-30-day-7-dd9 %}

###Blog on Day-6 of javascript30
{% link https://dev.to/cenacr007_harsh/javascript-30-day-6-103k %}

[Follow me on Twitter](https://twitter.com/cenacr007_harsh)
[Follow me on Linkedin](https://www.linkedin.com/in/kumarharsh22/)

**DEV Profile**

{% user cenacr007_harsh %}

You can also do the challenge at [javascript30](https://javascript30.com/)

Thanks @wesbos , [WesBos](https://github.com/wesbos) to share this with us! 😊💖

**Please comment and let me know your views**
##Thank You!