9 Console commands to make JavaScript easier to debug

Source: Internet
Author: User
Tags assert

I. Commands to display information
 <! DOCTYPE html><Html><Head><Title> Common Console commands</Title><Metahttp-equiv="Content-type"Content=</head> << Span class= "Hljs-name" >body> <script type= "Text/javascript" > console.log ( ' hello '); console.info ( ' information '); console.error ( ' error '); console.warn ( ' warning '); </script> </ body> </HTML>    

The most common is the console.log.

Two: Placeholder

Console The above concentration supports printf placeholder format, supported placeholders are: characters (%s), integers (%d or%i), floating-point numbers (%f), and Objects (%o)

<type="Text/javascript" >         console.log ("%d%d months%d days", 3, +);  </script>         

Effect:

Iii. Grouping of information

<! DOCTYPE html><Html><Head><Title> Common Console commands</Title><Metahttp-equiv="Content-type"Content="Text/html; Charset=utf-8 "/></Head><Body><ScriptType="Text/javascript" > Console.group ("first set of Information");     Console.log ("First Group One: Welcome to my Blog"); Console.log ("First group II: Hello!   ");    Console.groupend ();     Console.group ("Second set of Information");     Console.log ("second group first: Hello");    Console.log ("second group II: welcome you to join"); Console.groupend (); </script> </body> </html>        

Effect:

Iv. Viewing the information of an object

Console.dir () can display all properties and methods of an object.

<type="Text/javascript" >         var info = {blog:"http://www.ido321.com", Qqgroup:  259280570, message:Console.dir (info);  </script>        

Effect:

V. Display the contents of a node

Console.dirxml () is used to display the Html/xml code contained in a node of a Web page.

<! DOCTYPE html><Html><Head><Title> Common Console commands</Title><Metahttp-equiv="Content-type"Content="Text/html; Charset=utf-8 "/></Head><Body><DivId="Info" ><h3> Welcome to view my blog </ h3> <p> hope to communicate with each other </p> </ div> <script type=  "Text/javascript" > var info = Span class= "hljs-built_in" >document.getelementbyid ( ' info '); console.dirxml (info); </script> </ body> </HTML>    

Effect:

Vi. Judging if the variable is true

Console.assert () is used to determine whether an expression or variable is true. If the result is no, a corresponding message is output in the console and an exception is thrown.

<type="Text/javascript" >     console.assert (1==2, "error");// Will error
</script>

1 equals 2, error errors, assert (assertion) is a good feature to ensure the correctness of the program.

Tracing the call trajectory of the function.

Console.trace () is used to track the call path of a function.

<ScriptType="Text/javascript" > /* How the function is called, in which the Console.trace () method is available */functionadd (a,b) {console.trace ();  return a+b;   } var x = add3 (1,1); function ADD3 ( a,b)  {return add2 (A, b);} function ADD2 ( a,b)  {return add1 (A, b);} function ADD1 ( a,b) {return Add (b);} </SCRIPT>         

Console output Information:

Eight, chronograph function

Console.time () and Console.timeend () are used to display the elapsed time of the code.

<type="Text/javascript" >console.time ("console timer one"); For (var i=0;i<1000;i++) {for (var j=0;j<1000;j++) {}} Console.timee nd ("console timer one");  </script>                  

Run Time is 15.947ms

Performance analysis of Console.profile ()

Performance analysis (Profiler) is the analysis of the various parts of the running time, to find out the bottleneck, the method used is Console.profile ().

 <ScriptType="Text/javascript" >         functionAll () {alert (11);Forvar i=0;i<10;i++)      {Funca (1000);}    FUNCB (10000); } function funcA (count) {for (var i=0 ; i<count;i++) {}} function  FUNCB (count) {for (var I=0;i<count;i++) {}} console.profile (   ' Performance Analyzer ');   All (); console.profileend (); </SCRIPT>         

Output

9 Console commands to make JavaScript easier to debug

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.