{"id":1577,"date":"2012-06-04T09:01:56","date_gmt":"2012-06-04T16:01:56","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=1577"},"modified":"2012-06-07T12:49:51","modified_gmt":"2012-06-07T19:49:51","slug":"javascript-debugging-console","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/04\/javascript-debugging-console\/","title":{"rendered":"JavaScript Debugging With The console Object"},"content":{"rendered":"<p>In the early days of JavaScript there were few options for debugging large web applications.  You could have used the multiple alert notifications or possibly even calling document.write to write messages into the document.  Firebug changed web developers lives, and I think how we view JavaScript, by making it incredibly easy to debug HTML, CSS, and JavaScript.  Since Firebug, most current versions of web browsers include powerful debugging tools.  In addition to the debugging tools browsers have made available a <strong>JavaScript console object<\/strong> to help write debug statements.  With console, there is no need to pop up alerts to inspect the state of an object.  To write a log message intended for the console, just write the following JavaScript statement in the right place.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.log(&quot;This is a log message&quot;);\r\nconsole.log(&quot;Message one&quot;, &quot;Message two&quot;, &quot;Message three&quot;);\r\n<\/pre>\n<p>To open the console, you can find it in Safari by looking for the the Web Inspector, in Chrome it&#8217;s called Developer Tools, and in Firefox you&#8217;ll find it under the Tools menu as Web Console.<\/p>\n<p>The console object has a lot of useful debugging messages for errors with different severity, such as log, debug, info, warn, and error.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.info(&quot;I'm here.&quot;);\r\nconsole.warn(&quot;Do I smell smoke?&quot;);\r\nconsole.error(&quot;The roof, the roof, the roof is on fire.&quot;);\r\n<\/pre>\n<p>In addition to logging String messages, you can log whole JavaScript objects.  It may be useful to inspect a complete object and it&#8217;s state.   To write out the state of an object to the console use the dir method as in the following example.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.dir({fname:&quot;Bill&quot;, lname:&quot;Gates&quot;});\r\nconsole.dir(document.getElementById('SomeElementId'));\r\n<\/pre>\n<p>In Chrome and Safari, you can also use dirxml to display the a HTML element object in the console as HTML.  In my version of Firefox, this method did not work.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.dirxml(document.getElementById('SomeElementId'));\r\n<\/pre>\n<p>You can also group related log messages, no matter their severity.  To group console messages surround them with a group and groupEnd.  You can even nest groups inside of groups.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.group(&quot;Server update&quot;);\r\nconsole.log(&quot;calling server...&quot;);\r\n\/\/ ... any number of console log messages.\r\nconsole.groupEnd();\r\n<\/pre>\n<p>You can also test the performance of your JavaScript code with time and timeEnd.  Both of these methods accept a String label and it needs it needs to be the same value for both methods.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.time(&quot;Process Data&quot;);\r\n\/\/ ... some time consuming code\r\nconsole.timeEnd(&quot;Process Data&quot;);\r\n<\/pre>\n<p>When the timeEnd method is executed it will generate a message in the log console with the amount of time it took to run the code between the time and timeEnd method.<\/p>\n<div id=\"attachment_1581\" style=\"width: 545px\" class=\"wp-caption aligncenter\"><span class=\"frame-outer  size-large wp-image-1581\"><span><span><span><span><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1581\" data-attachment-id=\"1581\" data-permalink=\"http:\/\/juixe.com\/techknow\/index.php\/2012\/06\/04\/javascript-debugging-console\/chromedevelopertools\/\" data-orig-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools.png\" data-orig-size=\"908,432\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Chrome Developer Tools\" data-image-description=\"&lt;p&gt;The web console is part of Chrome&#8217;s Developer Tools.&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Chrome Developer Tools Web Console&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools-300x142.png\" data-large-file=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools-535x254.png\" src=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools-535x254.png\" alt=\"Chrome Developer Tools Web Console\" title=\"Chrome Developer Tools\" width=\"415\" height=\"197\" class=\"size-large wp-image-1581\" srcset=\"http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools-535x254.png 535w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools-300x142.png 300w, http:\/\/juixe.com\/techknow\/wp-content\/uploads\/2012\/06\/ChromeDeveloperTools.png 908w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/span><\/span><\/span><\/span><\/span><p id=\"caption-attachment-1581\" class=\"wp-caption-text\">Chrome Developer Tools Web Console<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the early days of JavaScript there were few options for debugging large web applications. You could have used the multiple alert notifications or possibly even calling document.write to write messages into the document. Firebug changed web developers lives, and I think how we view JavaScript, by making it incredibly easy to debug HTML, CSS, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[13,16,3,4],"tags":[699,137,46,702,102,703,333,700,701,800],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-pr","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1577"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=1577"}],"version-history":[{"count":5,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1577\/revisions"}],"predecessor-version":[{"id":1579,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1577\/revisions\/1579"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=1577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=1577"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}