console.log()
. As a matter of fact, this command is a simple debugging tool that allows you to output something to the console. However, the knowledge of some features of this tool will allow those who use it to increase the efficiency of work.console.log()
is, for example, to output some string or object. For example, print the following line in the console: console.log('Is this working?');
const foo = { id: 1, verified: true, color: 'green' }; const bar = { id: 2, verified: false, color: 'red' };
console.log(variable)
to solve this problem. Although the data in the console and get, with their output, it becomes clear one problem.foo
and bar
, are not here. Objects, using the arrow icon in the left parts of the lines, can be expanded, but even so, looking at the internal structure of objects, it can be very difficult to understand exactly which object is displayed in the console. In solving this problem, we can use the calculated names of the properties of objects. Namely, this feature of object literals, which appeared in ES6, allows using a convenient construction of the following type: console.log({ foo, bar });
console.log()
calls used earlier to display objects separately.console.table()
, you can use the command console.table()
. This is how the result of executing a console.table({ foo, bar })
command console.table({ foo, bar })
looks like. console.group('User Details'); console.log('name: John Doe'); console.log('job: Software Developer'); // console.group('Address'); console.log('Street: 123 Townsend Street'); console.log('City: San Francisco'); console.log('State: CA'); console.groupEnd(); console.groupEnd();
console.group()
command, groups, by default, are displayed in expanded form. In order to display them minimized, instead of this command, you can use the command console.groupCollapsed()
. In order to view the contents of such a group, you will need to expand it using the icon to the left of the group name.console.warn()
and console.error()
commands may be useful to you in order to emphasize the importance of some messages output to the console. They are used respectively for displaying warnings and errors.console.info()
command, which is designed to display informational messages.%c
directive. This can be useful, for example, for organizing the visual separation of information coming from subsystems of executing calls to certain APIs, from subsystems responsible for processing events generated by the user, and so on. The main thing here is to work out certain rules of stylization and adhere to them. Here is an example of setting the appearance of data displayed in the console: console.log('%c Auth ', 'color: white; background-color: #2274A5', 'Login page rendered'); console.log('%c GraphQL ', 'color: white; background-color: #95B46A', 'Get user details'); console.log('%c Error ', 'color: white; background-color: #D33F49', 'Error getting user details');
font-size
and font-style
.console.trace()
command displays the results of the stack trace to the console and allows you to judge what happened in a certain place of the program during its execution. For example, there are some methods that, in certain situations, need to be called only once, say - methods for deleting information from the database. You can check whether a single call of this method is executed only once, using console.trace()
. This command allows you to display information in the console that helps to monitor the correctness of the internal mechanisms of programs.console.time()
command allows you to measure the execution time of operations and output what was found out to the console. For example, we examine a couple of cycles with this command: let i = 0; console.time("While loop"); while (i < 1000000) { i++; } console.timeEnd("While loop"); console.time("For loop"); for (i = 0; i < 1000000; i++) { // } console.timeEnd("For loop");
Source: https://habr.com/ru/post/448920/
All Articles