{"id":33590,"date":"2016-04-13T16:15:56","date_gmt":"2016-04-13T10:45:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=33590"},"modified":"2016-12-19T15:16:57","modified_gmt":"2016-12-19T09:46:57","slug":"getting-to-know-the-console-api-for-better-debugging","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-to-know-the-console-api-for-better-debugging\/","title":{"rendered":"Getting to know the Console API for better debugging"},"content":{"rendered":"<p>Developer tools are powerful tools to debugging javascript file in <a title=\"frontend web development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">development of front-end web applications<\/a>. The console has an API that provides a number of methods that make debugging easier. There has a multiple method for debugging JavaScript files in web applications. Debug your web applications using the methods provided by the console API but some browsers do not support this functionality. It&#8217;s had a browser compatibility issue.<\/p>\n<p>In this blog post you\u2019re going to learn how to debug JavaScript files in your web applications using these methods provided by the console API.<\/p>\n<h3>Console.log():<\/h3>\n<p>In debugging, console.log() are most frequently used by web developers. Using this method to print a log messages in the console window. If you have an object and want to populate loges to use console.log() and passed variable as an attribute.<\/p>\n<p>[java]<br \/>\nvar country = [<br \/>\n\t{ countryCode: &amp;quot;93&amp;quot;, countryName: &amp;quot;Afghanistan&amp;quot;, capital: &amp;quot;Kabul&amp;quot;},<br \/>\n\t{ countryCode: &amp;quot;355&amp;quot;, countryName: &amp;quot;Albania&amp;quot;, capital: &amp;quot;Tirana&amp;quot;},<br \/>\n\t{ countryCode: &amp;quot;213&amp;quot;, countryName: &amp;quot;Algeria&amp;quot;, capital: &amp;quot;Algiers&amp;quot;},<br \/>\n\t{ countryCode: &amp;quot;684&amp;quot;, countryName: &amp;quot;American Samoa&amp;quot;, capital: &amp;quot;Pago Pago&amp;quot;}<br \/>\n];<\/p>\n<p>console.log(country);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33613\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-log.jpg\" alt=\"console-log\" width=\"286\" height=\"184\" \/><\/p>\n<h3>Console.table() :<\/h3>\n<p>In debugging we face more difficulties to find undefined values because few of the keys in object may be undefined values and that takes more time to debuggers. Using this method to show all data in tabular format and all the key and value shows in tabular format. Still, the property values are neatly arranged and give you a good overview.<\/p>\n<p>[java]<br \/>\nvar country = [<br \/>\n{ countryCode: &amp;quot;93&amp;quot;, countryName: &amp;quot;Afghanistan&amp;quot;, capital: &amp;quot;Kabul&amp;quot;},<br \/>\n{ countryCode: &amp;quot;355&amp;quot;, countryName: &amp;quot;Albania&amp;quot;, capital: &amp;quot;Tirana&amp;quot;},<br \/>\n{ countryCode: &amp;quot;213&amp;quot;, countryName: &amp;quot;Algeria&amp;quot;, capital: &amp;quot;Algiers&amp;quot;},<br \/>\n{ countryCode: &amp;quot;684&amp;quot;, countryName: &amp;quot;American Samoa&amp;quot;, capital: &amp;quot;Pago Pago&amp;quot;}<br \/>\n];<\/p>\n<p>console.table(country);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33615\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-table.jpg\" alt=\"console-table\" width=\"300\" height=\"58\" \/><\/p>\n<h3>Console.trace() :<\/h3>\n<p>Javascript is not a very structured language, it can sometimes be hard to get an overview of what happened and when because the views are created, multiple events are triggering functions and in the end you want to know what\/why caused this function call. In Javascript console have a trace method to solve this type of issues. It&#8217;s arranged all the calls and shows in a console window.<\/p>\n<p>[java]<br \/>\nvar mobile;<br \/>\nvar func1 = function(){<br \/>\nfunc2();<br \/>\n}<\/p>\n<p>var func2 = function(){<br \/>\nmobile = new Mobile();<br \/>\nmobile.funcX();<br \/>\n}<\/p>\n<p>var Mobile = function(){<br \/>\nthis.brand = &#8216;motorola&#8217;;<br \/>\nthis.color = &#8216;black&#8217;;<\/p>\n<p>this.funcX = function(){<br \/>\nthis.funcY();<br \/>\n}<\/p>\n<p>this.funcY = function(){<br \/>\nconsole.trace(&#8216;trace car&#8217;);<br \/>\n}<br \/>\n}<\/p>\n<p>func1();<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33617\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-trace.jpg\" alt=\"console-trace\" width=\"300\" height=\"140\" \/><\/p>\n<h3>console.count(label) :<\/h3>\n<p>The console.count() method will output the number of times that the count() method has been called. This method can be useful for finding out how many times a function is being called in your code.<\/p>\n<p>[java]<br \/>\nfunction counter() {<br \/>\nconsole.count(&#8216;counter called&#8217;);<br \/>\n}<\/p>\n<p>for (var i = 0; i &amp;lt; 10; i++) {<br \/>\ncounter();<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-33609\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-count.jpg\" alt=\"console-count\" width=\"159\" height=\"193\" \/><\/p>\n<h3>console.time() &amp; console.timeEnd() :<\/h3>\n<p>The console.time() and console.timeEnd() method gives a way to how long it takes for a piece of code to execute. Both the time() and timeEnd() methods should be passed the same label parameter.<\/p>\n<p>[java]<br \/>\nconsole.time(&#8216;time counter&#8217;);<br \/>\nfunction counter() {<br \/>\nconsole.count(&#8216;counter called&#8217;);<br \/>\n}<br \/>\nfor (var i = 0; i &amp;lt; 10; i++) {<br \/>\ncounter();<br \/>\n}<br \/>\nconsole.timeEnd(&#8216;time counter&#8217;);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33616\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-time.jpg\" alt=\"console-time\" width=\"164\" height=\"212\" \/><\/p>\n<h3>Console.group() , console.groupCollapsed() and console.groupEnd() :<\/h3>\n<p>The console.group() method is used to group together a series of log messages. Once this method is called, any further log messages will be added to the group until console groupEnd() is executed to close the group.<\/p>\n<p>The console.groupCollapsed() method is same as console.group() except that the group is initially displayed collapsed rather than open in the console.<\/p>\n<p>[java]<br \/>\nconsole.group(&amp;quot;Log Details&amp;quot;);<br \/>\nconsole.log(&amp;quot;Log 1&amp;quot;)<br \/>\nconsole.log(&amp;quot;Log 2&amp;quot;)<br \/>\nconsole.log(&amp;quot;Log 3&amp;quot;)<br \/>\nconsole.log(&amp;quot;Log 4&amp;quot;)<br \/>\nconsole.log(&amp;quot;Log 5&amp;quot;)<br \/>\nconsole.groupEnd();<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33612\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-group.jpg\" alt=\"console-group\" width=\"162\" height=\"117\" \/><\/p>\n<h3>console.dir() :<\/h3>\n<p>This method prints a JS representation of the define object. It is especially useful for examining HTML elements, as it will display the DOM representation of the element.<\/p>\n<p>[java]<br \/>\nconsole.dir(document.body);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33610\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-dir.jpg\" alt=\"console-dir\" width=\"300\" height=\"194\" \/><\/p>\n<h3>console.dirxml(object) :<\/h3>\n<p>This method displays an interactive tree of the descendant elements of the specified XML\/HTML element. The output is presented as a hierarchical listing of expandable nodes that let you see the contents of child nodes.<\/p>\n<p>[java]<br \/>\nconsole.dirxml(document.body);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33611\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-dirxml.jpg\" alt=\"console-dirxml\" width=\"300\" height=\"134\" \/><\/p>\n<h3>console.log(&#8216;%c&#8217;) :<\/h3>\n<p>To make interactive console output in console window, have a styling %c format specifier to allow to style in console output.<\/p>\n<p>[java]<br \/>\nconsole.log(&#8216;%c CSS in Console log&#8217;, &#8216;color: #ffffff; background-color: #999999; font-size: 20px; padding: 5px;&#8217;);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33614\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/console-style.jpg\" alt=\"console-style\" width=\"300\" height=\"23\" \/><\/p>\n<h3>console.assert() :<\/h3>\n<p>The assert method accepts two parameters a boolean expression, which references whether your test passed or failed and a short description of your test. When expression is false, then it&#8217;s shows defined message.<\/p>\n<p>[java]<br \/>\nvar count = 5;<br \/>\nconsole.assert(count &amp;gt; 10, &#8216;count is not larger than 10&#8217;);<br \/>\n[\/java]<\/p>\n<h3>console.info() :<\/h3>\n<p>This method in the same as console.log() but it\u2019s a show with info flag. This can be used to filter log messages using flags.<\/p>\n<p>[java]<br \/>\nconsole.info(&#8216;Hello Treehouse&#8217;);<br \/>\n[\/java]<\/p>\n<h3>console.warn() :<\/h3>\n<p>This method shows log message in the console windows with warning flag.<\/p>\n<p>[java]<br \/>\nconsole.warn(&#8216;This is warning message);<br \/>\n[\/java]<\/p>\n<h3>console.clear() :<\/h3>\n<p>The console.clear() method clears the console window.<\/p>\n<p>[java]<br \/>\nconsole.clear();<br \/>\n[\/java]<\/p>\n<h3>monitor() :<\/h3>\n<p>Monitors the function call and its arguments. Every time the function is called, it will be logged with the values that was passed in function.<\/p>\n<p>[java]<br \/>\nvar calculator = function(x,y,z){<br \/>\n};<br \/>\nmonitor(calculator);<\/p>\n<p>calculator (1,2);<br \/>\n[\/java]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-33618\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/monitor.jpg\" alt=\"monitor\" width=\"300\" height=\"17\" \/><\/p>\n<h4>For more:<\/h4>\n<p>github reference : <\/p>\n<p>Hope this will help!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developer tools are powerful tools to debugging javascript file in development of front-end web applications. The console has an API that provides a number of methods that make debugging easier. There has a multiple method for debugging JavaScript files in web applications. Debug your web applications using the methods provided by the console API but [&hellip;]<\/p>\n","protected":false},"author":370,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[3198,3195,3196,3199,3197],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33590"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/370"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=33590"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33590\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=33590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=33590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=33590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}