Table of Contents
1. console.trace()
2. console.time() && console.timeEnd()
3. console.memory
4. console.profile(‘profileName’) & console.profileEnd(‘profileName’)
5. console.count(“STUFF I COUNT”)
6. console.assert(false, “Log me!”)
7. console.group('group') & console.groupEnd('group')
8. String substitutions
9. console.clear()
10. console.table()
Home Web Front-end JS Tutorial 10 Advanced Tips for Using Console for JavaScript Debugging

10 Advanced Tips for Using Console for JavaScript Debugging

Sep 28, 2020 pm 05:39 PM
console debug javascript

10 Advanced Tips for Using Console for JavaScript Debugging

Over the past ten years, one of my biggest passions has been front-end development (specifically JavaScript). As a "craftsman", I like to specialize in various tools. In this article, I will introduce you to some debugging techniques using the old-fashioned console.

Yes, we all know the following basic skills:

console.log(‘Hello World!’);
console.info(‘Something happened…’); 
console.warn(‘Something strange happened…’); 
console.error(‘Something horrible happened…’);
Copy after login

From now on, I will teach you some skills you don’t know and let you become an experienced driver!

1. console.trace()

If you want to know where the message is printed, use console.trace() to get the stacktrace of the data to be printed. .


2. console.time() && console.timeEnd()

If you want to analyze the performance of the function, you can use console.time() to time, console.timeEnd() to end the time, the console will print out the time difference between the two times.

3. console.memory

If you find that the performance problem is difficult to analyze, and you may also want to consider whether there is a memory leak, you can use console.memory (note that memory is a property of console, not a function) to check the current heap usage.

Fundebug helps you debug better, welcome to try it!

4. console.profile(‘profileName’) & console.profileEnd(‘profileName’)

Although it is not a standard approach, it is widely accepted and used. You can use these two commands to start and stop profiling. This helps you do accurate profiling in your code. Rather than relying on manual mouse clicks. You can find the profile just now in the browser consoleJavascript Profiler.

5. console.count(“STUFF I COUNT”)

Sometimes in order to record how many times a function or a piece of code has been executed repeatedly, you can Use console.count('?') to record. Every time this code is executed, it will automatically increase by 1.

6. console.assert(false, “Log me!”)

You can use console.assert to Output messages if some conditions are false instead of using if-else.
Note: An Assertion Error will be reported under Node.js.

7. console.group('group') & console.groupEnd('group')

If you want to format the printed log For organized organization, you can use console.group() and console.groupEnd(). Use console.group to aggregate logs into groups and form nested hierarchies. Please see the example:

8. String substitutions

You can use console.log to print variables (%s = string, % i = integer, %o = object, %f = float).

9. console.clear()

We have output a lot of records on the console, use console.clear()Clear it.

10. console.table()

The last one! You can use console.table() to print the object in table form.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of 10 Advanced Tips for Using Console for JavaScript Debugging. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Pre-orders open for new Nintendo Switch Lite refresh Pre-orders open for new Nintendo Switch Lite refresh Jun 29, 2024 am 06:49 AM

Nintendo has opened pre-orders for the latest version of the Switch Lite (curr. $189.99 on Amazon). However, the device is not available to order globally just yet. To recap, the company presented the Switch Lite Hyrule Edition almost two weeks ago d

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Nintendo announces new Switch Lite refresh before Switch 2 release Nintendo announces new Switch Lite refresh before Switch 2 release Jun 20, 2024 am 09:41 AM

Nintendo presented plenty of games yesterday during its most recent Nintendo Direct event, an overview of which we have provided separately. Additionally, the company also announced a new version of the Switch Lite (curr. $194.93 on Amazon), possibly

MagicX XU Mini M: Teardown reveals RK3326 CPU instead of advertised RK3562, MagicX severs ties with 3rd-party dev MagicX XU Mini M: Teardown reveals RK3326 CPU instead of advertised RK3562, MagicX severs ties with 3rd-party dev Sep 01, 2024 am 06:30 AM

If you purchased the MagicX XU Mini M recently, this news might come as a surprise. A hardware and software teardown of the newly released handheld console revealed that the advertised RK3562 CPU is, in fact, a lower-specced, older RK3326 processor.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles