Home > Web Front-end > JS Tutorial > body text

How to use the JavaScript console to improve your workflow

不言
Release: 2018-07-14 10:52:48
Original
1287 people have browsed it

This article mainly introduces how to use the JavaScript console to improve the work process. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

As a web developer, it is very important It is necessary to know how to debug the code. In back-end development, we often use external libraries to record logs and format and display logs in some cases. On the front-end, we use breakpoints and consoles, but the console of our browser is much more powerful than we think.

When we think about the console, the first thing that comes to mind is console.log, right? But there are many more methods than we think. Now let's take a look at how to get the most out of the console, and I'll give you some tips to make these methods more readable

What is the console?

The JavaScript console is a built-in feature in modern browsers that comes with out-of-the-box development tools in a shell-like interface. It allows developers to:

  • View logs of errors and warnings that occur on web pages.

  • Use JavaScript commands to interact with web pages.

  • Debug your application and traverse the DOM directly in the browser.

  • Inspect and analyze network activity

Basically, it enables you to write, manage and monitor JavaScript in your browser.

Console.log, Console.error, Console.warn and Console.info
These are probably the most commonly used methods. You can pass multiple parameters to these methods. Each argument is evaluated and concatenated in a space-delimited string, but for objects or arrays you can navigate between their properties.

How to use the JavaScript console to improve your workflow

Console.group

We may use a lot of console.log() to detect when checking the code logic and process, but you will find that the control There are a lot of printouts from the station. This method allows you to group a series of console.logs (and error messages, etc.) under collapsible groups. The syntax is very simple: just console.log everything we want to group before console.group() (or console.groupCollapsed() if we want it to be closed by default). Then console.groupEnd() adds a closing group at the end.
How to use the JavaScript console to improve your workflow

How to use the JavaScript console to improve your workflow

Console.table

My life has changed since I discovered console.table. Displaying JSON or very large JSON arrays inside a console.log is a bad experience. This console.table allows us to visualize these structures in a nice table where we can name the columns and pass them as parameters.

How to use the JavaScript console to improve your workflow

Very nice and very useful in debugging:

How to use the JavaScript console to improve your workflow

##Console.count, Console.time and Console.timeEnd

These three methods are a Swiss Army Knife for every developer who needs to debug. The console.count counts and the output is the number of times count() has been called on the same line, and with the same label. The console.time starts the timer with the name specified as the input parameter and can run up to 10,000 timers simultaneously on a specific web page. Once started, we use a call to console.timeEnd to stop the timer and print the elapsed time to the console.

How to use the JavaScript console to improve your workflow

The output will look like this:

How to use the JavaScript console to improve your workflow

Console.trace and Console.assert

these The method simply prints the stack trace from the code location where it was called. Imagine you are creating a JS library and want to notify the user where errors are generated. In this case, these methods are very useful. The console.assert is like console.trace, but only prints the conditions that are not met.

How to use the JavaScript console to improve your workflow

As we can see, the output is exactly what React (or any other library) shows us when an exception is generated.

How to use the JavaScript console to improve your workflow

Delete all consoles

The above is the detailed content of How to use the JavaScript console to improve your workflow. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!