Project 9: Must Know Dev Tools Tricks
Tags: javascript, webdev, beginners, programming
Published: 2021-12-09 18:36:52

Welcome to my "Build 30 Js Projects in 30 Days" Series .This is day 9 and project 9. If you haven't read the other articles in this series please check them out first. I'll list them at the end of this article.

As mentioned in my previous article. This is the Day 9 challenge of [Wes Bos Javascript30 course](https://javascript30.com/).


As always before starting download the starter files from [here](https://github.com/wesbos/JavaScript30). I've made a separate article on how to download starter files, you can check it out [here](https://dev.to/prachigarg19/build-30-javascript-projects-in-30-daysday-0-2m8j).

This is a theoretical challenge where we will discuss some helpful dev tool tricks.
1. **<u>Suppose we have javascript on our page and we want to see the js code acting on an element</u>** then we can simply select inspect the elements > go to the html code of that element > Right click >Break on> Attribute modification. This will pause our website when js code is implemented on that element and shows that particular line with a dot left to it.This can help a lot when we see websites with large code bases and cannot figure out the js code acting on a element.

Below we will discuss about different types of output that can be printed on console other than our regular console.log. Hope you find these helpful too!

1. **<u>Ways to print variable values in console-</u>**

   a. console.log("My name is %s",'Prachi');
   b. var="Prachi"
     console.log("My name is ${var}");

2.**<u>Applying css on our console output-</u>**
 `console.log("%c This is styled text","color:red;font-size:20px");`
<u>First argument</u>- %c and statement to be printed
<u>Second argument</u>-css to be applied to the statement.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/64dzl7ocz4uvcs75wtfz.png)

3.**<u>Printing warning message- </u>**
console.warn('This is a warning');

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iitvlwz281jy4n1i8qqs.png)
It also displays the stack trace from where it was called.

4.**<u>Displaying error message:</u>**
console.error('This is a error");
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w01n7kf58o8ihhn7jxmd.png)
It also displays the stack trace from where it was called.

5.**<u>Displaying statement with an info sign next to it:</u>**
console.info('This is info');

6.**<u>To check if statement is true or not:</u>**
Suppose we want to check if a part of our code is true or not then we can use this trick.
e.g. <u>we want to check is our input has attribute value or not then-</u>

```
<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>
      
      <input type="text" id="text">
      <script>
       text=document.getElementById('text');
        console.assert(text.hasAttribute('value'),"There is no value attribute");
      </script>
  </body>
</html>
```
OUTPUT-
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs1isq8s3b8bf0tr8zob.png)

assert will have 2 arguments, first will contain statement that is to be checked,second will be the statement that we want to print for displaying error. Also, assert shows output ONLY IF STATEMENT IS WRONG.

7.**<u>Clearing console:</u>** 

console.clear(); 

8.**<u>Displaying all the properties and elements associated with an element:</u>**

console.log(element name);
e.g.

```
<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>
      
     <p id="text">lorem10</p>
      <script>
       text=document.getElementById('text');
        console.dir(text);
      </script>
  </body>
</html>
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frrp50bl7o03igvh7ceh.png)

9.**<u>Grouping multiple console statements.</u>**
Suppose we want to iterate over our array and group all the statements for better readability,then-

```
<!DOCTYPE html>
<html>
  <head>
    
  </head>
  <body>
   
      <script>
       let Dogs=[{name:"Mylo",age:2},{name:"Noddy",age:3},{name:"blacky",age:4}];
       
       for(dog of Dogs)
       {  //naming of group
         console.group(`${dog.name}`);
         console.log(`Hello my name is ${dog.name}`);
         console.log(`I am ${dog.age} years old`);
         //ending group
         console.groupEnd(`${dog.name}`);
       }
      </script>
  </body>
</html>
```
OUTPUT:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3hvex1rc5wa9r5yigxm.png)
We can have different argument value in group and groupEnd.

Without groupEnd our first group won't end and the next object will be shown as a subgroup of the first group-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpf5kd3lvfbeidnz2azk.png)
 
10.**<u>Printing the number of times a particular dom element,statement,variable etc. has been printed on console:</u>**

```
      console.count('Mylo');
      console.count('Noddy'); 
      console.count('Mylo');
      console.count('Mylo');
```
OUTPUT:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvb7n3am7wryvj4gtept.png)
 
11. **<u>Displaying content in table format:</u>**

```
let Dogs=[{name:"Mylo",age:2},{name:"Noddy",age:3},{name:"blacky",age:4}];
       console.table(Dogs)
```
OUTPUT:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yeospi8cflecln20nm6.png)

12.**<u>Displaying time taken by a particular set of code.</u>**
We will use console.time to begin with time recording and console.timeEnd to end recording and displaying time taken.
E.g.

```
let Dogs=[{name:"Mylo",age:2},{name:"Noddy",age:3},{name:"blacky",age:4}];
       
       //start timer
       console.time('Iterating array')
       for(dog of Dogs)
       {  
         console.log(`${dog.name}`);
        }
        //ending and displaying time
       console.timeEnd('Iterating array');
```
OUTPUT-
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4oot0blztlx8prjld3z.png)
Also time and timeEnd should have same string otherwise it'll show a warning displaying that the string doesn't exist unlike group and groupEnd where different arguments will work. 
 
##<u>Things I learnt:</u>
Almost all the tricks mentioned in this article were new to me.

## <u>Previous article from this series:</u>
[Day 8 Project 8](https://dev.to/prachigarg19/project-8html-5-canvas-7bg), in this project I built a HTML5 canvas. Do check it out if you haven't yet.

## <u>Conclusion</u>

That's it for today's project.Next project will be 'Hold shift to check multiple checkboxes'.

If you have any doubts or suggestions please do let me know in the comment section. I'll be more than happy to interact with you.

If you like this series and want to be a part of it, do consider following me at @prachigarg19

Thanks for reading. :)