Content provider: Jinniu District Wudi Software Development Studio

Console is one of the most commonly used commands in front-end development, but most people only know console.log(), and do not know that console has many other operations.

Console. log([data]:any[,…args]:any) is used to check whether the parameters of the function are the values we want or the result is the expected value.

console.log("Oh, Xiao Di wu.")   // You can print strings directly
console.log(1)           // Output the number directly
var a = ["1"."2"."3"]
console.log(a)           // Output variables
function test(a,b){
   console.log(a,b)      // Output function arguments
}
test(1."2")
Copy the code

The console output of the above code is as follows: 2. Console.clear () This method is used to clear the contents of the console. It can be used when there are too many things on the console, or when I want the current output to be displayed at the top, and its previous output is temporarily hidden:

// Concatenate this code below the previous code
console.clear()
console.log("All the others must have disappeared? I'm the boss now!")
Copy the code

At this point, you’ll notice that the previous output is gone, except for the output after console.clear(). 3. Console. time([label] : string) This is used in conjunction with console.timeEnd([label] : string), which front-end developers use when they want to test performance themselves, for example I want to test how long it takes to add several values to an empty array.

var arr = [];
console.time("test")
for(let i = 0; i <1000000; i++){// Add 1,000,000 values to the array
    arr.push(i)
}
console.timeEnd("test")
Copy the code

Eventually we find that it took 110.0439453125 milliseconds to add 1,000,000 values to the array Pay attention to: 1. If there are no arguments in console.time() and console.timeend () parentheses, the default:*** is printed; 2. The output time is not fixed, but will fluctuate with the performance of the computer. It is often used to test the execution time of an algorithm/function in order to select the best algorithm/function for comparison.

Console.count ([label] : string) is used to maintain a label-specific internal counter and to print to the console the number of times console.count() is called with a given label.

console.count()
console.count()
console.count("test")
console.count("test")
console.count()
Copy the code

Pay attention to: 1. As with console.time, do not give the parameter default 2. Even if it’s separated, it’s still going to keep counting at the same number as before

Console. countReset([label] : string) is used to reset the specified count for example:

console.count()
console.count()
console.countReset() // Reset the count
console.count()
Copy the code

The output is:You can see that the output default starts counting from 1 again after the reset.6. Console. dir(obj: any[,options] : Object) parameter:

  • obj
  • Options Options parameters:
    • ShowHidden, if true, also displays the object’s non-enumerable and symbolic properties. Default value: false
    • Depth tells util.inspect() how many times to recurse when formatting the object. This is useful for examining large complex objects. To make it infinitely recursive, you pass in NULL. Default value: 2.
    • Colors If true, the output will be styled using ANSI color codes. Colors are customizable, see custom util.inspect() colors. Default value: false.
Usage:

Use util.inspect() on obj and print the resulting string to stdout. This function bypasses any custom inspect() functions defined on obj. Console. error([data] : any[,…args] : any) prints to stderr with a newline character. Multiple arguments can be passed, with the first being used as the main information and all the others as alternative values similar to those in printf(3) (the arguments are passed to util.format())).

const code = '5';
const code2 = '6';
function test(a,b){
     if(typeof(a)==="string" || typeof(b)==="string") {console.error('error: Parameter A or b cannot be considered a string ')}else{
        console.log('Parameters are within permissible limits')
     }
}
test(code,code2)
Copy the code

Javascript itself is not the function parameter data types do judgment, no matter what is your incoming type, as long as you don’t violate compasses operation is not an error, but we can through the way similar to the above judgment to the data type of the constraint parameters, (judge data type there are a variety of ways, of course, please keep an eye on the next update, If the data type of a parameter is not what we want, we can warn it with an error Console. Warn ([data] : any[,…args] : any); console. Warn ([data] : any[,…args] : any); The display effect is: 9. Console. table(tabularData:any[,properties]: String []) attempts to construct a table and record it using tabularData(or using properties) property columns and tabularData rows. If it cannot be parsed into a table, fall back to the forward record parameter.

// These cannot be parsed into tabular data.
console.table(Symbol());  / / symbl type
console.table(1)        / / number type
console.table("1")      / / type string
console.table(undefined)        / / undeined type
console.table(true)     / / a Boolean type
// Can be parsed to table data format:
console.table([{ a: 1.b: 'Y' }, { a: 'Z'.b: 2 }]);
Copy the code

Console output: 10. Console.debug (data[,…args]) is an alias for console.log()

11. Console.info (data[,…args]) is an alias for console.log()

12. console.dirxml(… Data) This method calls console.log() to pass it the parameters it receives. Note that this method does not generate any XML format

13. Console. group([…label]:any) Indent subsequent lines by two Spaces. If one or more labels are provided, they are printed first without additional indentation.

Collapsed(), console.group().

Console.groupend (), which reduces the indentation of subsequent lines by two Spaces.

16. Console.timelog ([label]:string[,…data]:any), prints the elapsed time and other data parameters to STdout for timers that were previously started by calling console.time()

Console. trace([message]:any[,…args]:any), prints the string ‘trace:’ to stderr, then prints the util.format() formatted message and stack trace to the current location in the code.

Change the style of console.log: console.log(‘%c your console content ‘, ‘your style content ‘)

console.log('% C hello, dear developer friend, I am the founder of Privileged APP. This project is also one of the projects of privileged APP team. If you are interested in our team and want to join us, please send your resume here: [email protected]. Privileged APP is looking forward to your joining! `.'font-size: 20px; color: orange; ')
console.log('%c '.'font-size: 20px; background: url("http://nowre.com/uploads/feature/feature_header_1435686903_FEATURE-TOP17.jpg"); background-size: 100%; ')
Copy the code

Effect:

Do not know friends to learn these SAO operation? Go to the code and try it!