The most basic and common method is console.log(), which prints the string passed to the console
You can also format terms by passing in variables and format specifiers
console.log("My %s is % D"."Cat".2);
Copy the code
- %s formats the variable as a string
- %d formats variables as numbers
- % I formats the variable as its integer part
- %o formats variables as objects
The smart ones already know what printing is… Ah, you don’t know, so try it
Clear console
Console.clear () clears the console
Element count
const x = 1
const y = 2
const z = 3
console.count('x 'is equal to 'x'. + x + 'And how many times has it been checked? ') // The value of x is 1 and has been checked several times? : 1.
console.count('x 'is equal to 'x'. + x + 'And how many times has it been checked? ') // The value of x is 1 and has been checked several times? : 2
console.count('y 'is equal to 'y'. + y + 'And how many times has it been checked? ') // The value of y is 2 and has been checked several times? : 1.
Copy the code
The count method counts the number of times a string is printed and prints a count next to it:
Print a stack trace
Trace
at function2 (repl:1:33)
at function1 (repl:1:25)
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:440:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
Copy the code
Console.trace () prints the function’s call stack trace
Computing time
Time () and timeEnd() can be used to easily calculate how long a function takes to run:
const doSomething = () = > console.log('test')
const measureDoingSomething = () = > {
console.time('doSomething()')
// Do something and measure how long it takes.
doSomething()
console.timeEnd('doSomething()')
}
measureDoingSomething()
Copy the code
Stdout and stderr console.log are great for printing messages in the console. This is known as standard output (or STDout). Console. error is printed to the stderr stream. It does not appear in the console, but in the error log.
Coloring output
It is available in a browser
console.log('%c this is colored'.'background:#aaa; color:#bada55'.'this is not colored');
Copy the code
- %c represents CSS styles
In the Node environment
You can use escape sequences to color the output of text in the console. An escape sequence is a set of characters that identify colors. Such as:
console.log('\x1b[33m%s\x1b[0m'.'hello')
Copy the code
Try this in the Node.js REPL, which prints hello in yellow. Of course, this is the underlying method of doing this. The easiest way to color console output is to use a library. Chalk is one such library that, in addition to coloring it, helps with other styles (such as making text bold, italic, or underlined). NPM Install Chalk can be used to install and then it can be used:
const chalk = require('chalk')<br/>
console.log(chalk.yellow('hello'))<br/>
Copy the code
Using chalk. Yellow is much more convenient and readable than trying to remember the escape code.