{"id":4165,"date":"2024-03-03T12:50:05","date_gmt":"2024-03-03T12:50:05","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=4165"},"modified":"2024-03-03T12:55:57","modified_gmt":"2024-03-03T12:55:57","slug":"using-javascript-console-better","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2024\/03\/03\/using-javascript-console-better\/","title":{"rendered":"Using JavaScript console better"},"content":{"rendered":"<p>The\u00a0<strong><code>console<\/code><\/strong>\u00a0object provides access to the debugging console (e.g., the\u00a0<a class=\"external\" href=\"https:\/\/firefox-source-docs.mozilla.org\/devtools-user\/web_console\/index.html\" target=\"_blank\" rel=\"noopener\">Web console<\/a>\u00a0in Firefox). The specifics of how it works vary from browser to browser or server runtimes (Node.js, for example), but there is a\u00a0<em>de facto<\/em>\u00a0set of features that are typically provided.<\/p>\n<p>The\u00a0<code>console<\/code>\u00a0object can be accessed from any global object.\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\"><code>Window<\/code><\/a>\u00a0on browsing scopes and\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WorkerGlobalScope\"><code>WorkerGlobalScope<\/code><\/a>\u00a0as specific variants in workers via the property console. It&rsquo;s exposed as\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/console\"><code>Window.console<\/code><\/a>, and can be referenced as\u00a0<code>console<\/code>.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a014005c3b69\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a014005c3b69\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/fr\/2024\/03\/03\/using-javascript-console-better\/#consoletable\" >console.table()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/fr\/2024\/03\/03\/using-javascript-console-better\/#consoledir\" >console.dir()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/fr\/2024\/03\/03\/using-javascript-console-better\/#consoletime-and-consoletimeend\" >console.time() and console.timeEnd()<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"consoletable\"><\/span>console.table()<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><strong>Usage<\/strong>: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.table()<\/code> allows us to generate a table inside a console. The input must be an array or an object which will be shown as a table.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let info = [[\"Tung\"], [\"Lily\"], [\"Tony\"], [\"Ethan\"]];\r\nconsole.table(info);<\/pre>\n<figure id=\"attachment_4169\" aria-describedby=\"caption-attachment-4169\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4169 size-large\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-table-1024x547.png\" alt=\"console.table - how to use it\" width=\"1024\" height=\"547\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-table-1024x547.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-table-300x160.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-table-768x410.png 768w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-table.png 1416w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4169\" class=\"wp-caption-text\"><em>console.table() &#8211; how to use this statement.<\/em><\/figcaption><\/figure>\n<h1><span class=\"ez-toc-section\" id=\"consoledir\"><\/span>console.dir()<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><strong>Usage<\/strong>: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.dir()<\/code> prints a JSON representation of the specified object.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let userData = { \r\n    \"username\": \"tnguyen\",\r\n    \"title\": \"Senior Software Engineer\",\r\n    \"email\": \"tung@itersdesktop.com\"\r\n};\r\nconsole.dir(userData);<\/pre>\n<figure id=\"attachment_4170\" aria-describedby=\"caption-attachment-4170\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-dir.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-4170\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-dir-1024x453.png\" alt=\"console.dir() - how to use it\" width=\"1024\" height=\"453\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-dir-1024x453.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-dir-300x133.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-dir-768x340.png 768w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-dir.png 1410w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4170\" class=\"wp-caption-text\"><em>console.dir() &#8211; how to use it<\/em><\/figcaption><\/figure>\n<h1><span class=\"ez-toc-section\" id=\"consoletime-and-consoletimeend\"><\/span>console.time() and console.timeEnd()<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><strong>Usage<\/strong>: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.time()<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.timeEnd()<\/code> are the ways of tracking the micro time taken for JavaScript executions.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.time(\"Time\");\r\n\r\nlet sum = 0;\r\nfor (let i = 1; i &lt;= 1000; i++) {\r\n  sum += i;\r\n}\r\nconsole.log(\"Sum: \", sum);\r\nconsole.timeEnd(\"Time\");<\/pre>\n<figure id=\"attachment_4171\" aria-describedby=\"caption-attachment-4171\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-time-timeEnd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-4171\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-time-timeEnd-1024x406.png\" alt=\"console.time() and console.timeEnd() - how to use them.\" width=\"1024\" height=\"406\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-time-timeEnd-1024x406.png 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-time-timeEnd-300x119.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-time-timeEnd-768x304.png 768w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2024\/03\/4165-console-time-timeEnd.png 1408w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4171\" class=\"wp-caption-text\"><em>console.time() and console.timeEnd() &#8211; how to use them.<\/em><\/figcaption><\/figure>\n<p>That&rsquo;s it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0console\u00a0object provides access to the debugging console (e.g., the\u00a0Web console\u00a0in Firefox). The specifics of how it works vary from browser to browser or server runtimes (Node.js, for example), but there&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[641,133],"tags":[684,535,568],"class_list":["post-4165","post","type-post","status-publish","format-standard","hentry","category-javascript","category-jquery","tag-console","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/4165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=4165"}],"version-history":[{"count":6,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/4165\/revisions"}],"predecessor-version":[{"id":4174,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/4165\/revisions\/4174"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=4165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=4165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=4165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}