{"id":12974,"date":"2022-10-30T23:46:05","date_gmt":"2022-10-30T18:16:05","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=12974"},"modified":"2022-10-30T23:46:07","modified_gmt":"2022-10-30T18:16:07","slug":"nodejs-console-methods","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/nodejs-console-methods\/","title":{"rendered":"NodeJS Console Methods &#8211; 8 Methods to Know"},"content":{"rendered":"\n<p>Node.js includes various global objects used to perform different operations. Global objects are global in nature, they can be used anywhere directly. One of the widely used global objects is the console object which is used literally in every Node.js project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NodeJS Console Methods<\/h2>\n\n\n\n<p>A console object is the global Node.js object used to print different types of values like information, error, warning, object\u2019s properties, time, etc.&nbsp;<\/p>\n\n\n\n<p>There are different methods of the console object in Node.js that we can use according to the application&#8217;s needs.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>console.log()<\/li><li>console.info()<\/li><li>console.error()<\/li><li>console.warn()<\/li><li>console.dir()<\/li><li>console.time()<\/li><li>console.timeEnd()<\/li><li>console.assert()<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">1. console.log()<\/h3>\n\n\n\n<p>This method is used to print a value in the console.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(msg)\n<\/pre><\/div>\n\n\n<p>Where <em>msg <\/em>is the value you want to show in the console.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(&quot;Log: Hello World!&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nLog: Hello World!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. console.info()<\/h3>\n\n\n\n<p>This method is used to print informational messages in the console. The difference between console.log and console.info is that some browsers display the <strong>\u201ci\u201d<\/strong> icon next to the information messages like firefox.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.info(msg)\n<\/pre><\/div>\n\n\n<p>Where <em>msg <\/em>is the information you want to display in the console.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.info(&quot;Info: Hello World!&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nInfo: Hello World!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. console.error()<\/h3>\n\n\n\n<p>This method is used to print errors in the console.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.error(err)\n<\/pre><\/div>\n\n\n<p>Where <em>err <\/em>is the error you want to display in the console.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.error(&quot;Error: Hello World!&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nError: Hello World!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. console.warn()<\/h3>\n\n\n\n<p>This method is used to print the warning in the console.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.warn(warning)\n<\/pre><\/div>\n\n\n<p>Where the <em>warning <\/em>is the warning you want to display in the console.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.warn(&quot;Warning: Hello World!&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nWarning: Hello World!\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. console.dir()<\/h3>\n\n\n\n<p>This method is used to print the properties of a JavaScript object in the console.<\/p>\n\n\n\n<p>We can also use console.log() for this but the difference between them is the console.dir() makes a copy of an object before logging whereas console.log() passes that object&#8217;s reference.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.dir(obj)\n<\/pre><\/div>\n\n\n<p>Where <em>obj <\/em>is the object which properties you want to display in the console.<\/p>\n\n\n\n<p><strong>Example:<\/strong> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst obj = {\n    firstLetter: &quot;Hello&quot;,\n    lastLetter: &quot;World!&quot;,\n}\n\nconsole.dir(obj)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n{ firstLetter: &#039;Hello&#039;, lastLetter: &#039;World!&#039; }\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">6. console.time()<\/h3>\n\n\n\n<p>This method starts a timer with the name specified as the augment.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.time(msg)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">7. console.timeEnd()<\/h3>\n\n\n\n<p>This method is used to stop the timer start by console.time() and show the recoded time from the start to the end in the console.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.timeEnd(msg)\n<\/pre><\/div>\n\n\n<p>Where <em>msg <\/em>is the value for which you stop the timer and display it in the console with time.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.time(&quot;Time&quot;)\nconsole.timeEnd(&quot;Time&quot;) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nTime: 0.943ms\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">8. console.assert()<\/h3>\n\n\n\n<p>This method performs assertion and if the assertion is false then it throws the error message in the console.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.assert(assertion, msg)\n<\/pre><\/div>\n\n\n<p>Where the <em>msg <\/em>can be a value you want to display in the console when the assertion is false.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.assert((4 == 2), &quot;4 is not equal to 2&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nAssertion failed: 4 is not equal to 2\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The console is one of the most important objects in JavaScript which can be directly called anywhere, it is a global object that comes with various methods used to perform different operations. It is used to show information, error, and object properties in the console. It is also used to trace the time in the console, print stack trace, etc. We hope this tutorial helped you understand the NodeJS console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/console\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/console<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/11954152\/whats-the-difference-between-console-dir-and-console-log\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/11954152\/whats-the-difference-between-console-dir-and-console-log<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node.js includes various global objects used to perform different operations. Global objects are global in nature, they can be used anywhere directly. One of the widely used global objects is the console object which is used literally in every Node.js project. NodeJS Console Methods A console object is the global Node.js object used to print [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":12975,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[14],"tags":[],"class_list":["post-12974","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail.png",1200,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail-300x150.png",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail-768x384.png",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail-1024x512.png",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail.png",1200,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2022\/10\/NodeJS-Console-Thumbnail.png",1200,600,false]},"uagb_author_info":{"display_name":"Aditya Gupta","author_link":"https:\/\/codeforgeek.com\/author\/aditya\/"},"uagb_comment_info":0,"uagb_excerpt":"Node.js includes various global objects used to perform different operations. Global objects are global in nature, they can be used anywhere directly. One of the widely used global objects is the console object which is used literally in every Node.js project. NodeJS Console Methods A console object is the global Node.js object used to print&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/12974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=12974"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/12974\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/12975"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=12974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=12974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=12974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}