

{"id":103452,"date":"2021-12-02T09:00:22","date_gmt":"2021-12-02T03:30:22","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=103452"},"modified":"2021-12-02T10:04:57","modified_gmt":"2021-12-02T04:34:57","slug":"nodejs-global-objects-with-examples","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/","title":{"rendered":"Nodejs Global Objects with Examples"},"content":{"rendered":"<p>In this article, we will be focusing on global objects. We will be looking in detail into the built-in global objects with codes and examples.<\/p>\n<h3>Nodejs Global Objects:<\/h3>\n<p>Global object means that these objects are accessible from anywhere, you can use this object from any module in your project.<\/p>\n<h3>Console:<\/h3>\n<p>It is used for debugging your code. Console will display the information in the output.<\/p>\n<h3>Nodejs Console class:<\/h3>\n<p>This class contains methods such as log, error etc.<\/p>\n<h4>Example for console in Nodejs:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">console.log('hello from DataFlair');\r\nconsole.error(new Error('An error has occurred'));\r\nconst name = 'Welcome to DataFlair';\r\nconsole.warn(`Danger ${name}! Danger!`);\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>hello from DataFlairAn error has occurred<\/p>\n<p>Welcome to DataFlair<\/p>\n<\/div>\n<h3>Nodejs Process:<\/h3>\n<p>It provides information about the program that is in execution.<\/p>\n<h4>Example of process in Nodejs<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">process.on('exit', function (code) {\r\n    setTimeout(function () {\r\n        console.log(\"DataFlair\");\r\n    }, 0);\r\n    console.log('exit code:', code);\r\n});\r\nconsole.log(\"Program Ended\");\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p>Program Ended<\/p>\n<p>exit code:0<\/p>\n<\/div>\n<h4>1. __filename:<\/h4>\n<p>It shows the filename being executed. It is the absolute path.<\/p>\n<p><strong>Example for filename:<\/strong><\/p>\n<p>To view the absolute path of your current file run the below line:<\/p>\n<p><strong>console.log(__filename)<\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">DataFlair\/index.js<\/div>\n<h4>2. __dirname:<\/h4>\n<p>It shows the current directory where the code is being executed. It is the absolute path.<\/p>\n<p><strong>Example for filename:<\/strong><\/p>\n<p>To view the absolute path of your current directory run the below line<\/p>\n<p><strong>console.log(__dirname)<\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">DataFlair<\/div>\n<h4>3. setTimeout:<\/h4>\n<p>It takes a callback function which gets completed only when the specified amount of time has passed.<\/p>\n<p><strong>Code for setTimeout<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function DataFlair() {\r\n    console.log(\"Welcome to DataFlair\");\r\n}\r\nsetTimeout((DataFlair), 2000);\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Welcome to DataFlair<\/div>\n<h4>4. clearTimeout:<\/h4>\n<p>If a timer has been set by setTimeout() then clearTimeout() will clear that timer.<\/p>\n<p><strong>Example for clearTimeout<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function DataFlair() {\r\n    console.log(\"Welcome to DataFlair\");\r\n}\r\nclearTimeout(setTimeout((DataFlair), 2000))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Nothing will be printed because cleartimeout will clear the timer set by the setTimeout.<\/p>\n<h4>5. setInterval:<\/h4>\n<p>It takes a callback function which executes every time after the specified amount of time has passed.<\/p>\n<p><strong>Example of setInterval<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function DataFlair() {\r\n    console.log(\"Welcome to DataFlair\");\r\n}\r\nsetInterval((DataFlair), 2000);\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>After every two seconds, welcome to DataFlair will be printed.<\/p>\n<h4>6. clearInterval:<\/h4>\n<p>If a timer has been set by setInterval() then clearInterval() will clear that timer.<\/p>\n<p><strong>Example for clearInterval<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">function DataFlair() {\r\n    console.log(\"Welcome to DataFlair\");\r\n}\r\nclearInterval(setInterval((DataFlair), 2000))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Nothing will be printed because clearInterval will clear the timer set by the setTimeout.<\/p>\n<h4>7. TextEncoder:<\/h4>\n<p>It encodes the given string using the utf-8.<\/p>\n<p><strong>Code for TextEncoder<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let encoder = new TextEncoder()\r\nconsole.log(encoder.encode(\"Welcome to DataFlair\"))\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103997\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder-1.webp\" alt=\"nodejs global encoder\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder-1.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder-1-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>8. TextDecoder:<\/h4>\n<p>It decodes the encoded string and returns the resultant string.<\/p>\n<p><strong>Code for TextDecoder<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let encoder = new TextEncoder()\r\nlet decoder = new TextDecoder()\r\nconsole.log(decoder.decode(encoder.encode(\"Welcome to DataFlair\")))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103995\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder.webp\" alt=\"nodejs global encoder\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-encoder-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>queueMicrotask:<\/h3>\n<p>It is a short function that is executed after the callback function is completed and only if the javascript execution stack is empty. The queueMicrotask() method is used to execute such functions after the callback function completes successfully.<\/p>\n<h3>Url class:<\/h3>\n<p>This class contains information about the url that is passed to it.<\/p>\n<p><strong>Code for url class<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let url = new URL(\"https:\/\/data-flair.training\/\")\r\nconsole.log(url)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-url.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103996\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-url.webp\" alt=\"nodejs global url class\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-url.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-url-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Web Assembly:<\/h3>\n<p>It acts as a namespace for W3C WebAssembly related features. It is a low level Assembly-like language that runs on modern browsers.<\/p>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have seen global objects in detail.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be focusing on global objects. We will be looking in detail into the built-in global objects with codes and examples. Nodejs Global Objects: Global object means that these objects&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":103994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25793],"class_list":["post-103452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-nodejs-global-objects"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nodejs Global Objects with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about Nodejs Global Objects. See built-in global objects with codes and examples for proper understanding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nodejs Global Objects with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about Nodejs Global Objects. See built-in global objects with codes and examples for proper understanding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-02T03:30:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-02T04:34:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-objects.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nodejs Global Objects with Examples - DataFlair","description":"Learn about Nodejs Global Objects. See built-in global objects with codes and examples for proper understanding.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/","og_locale":"en_US","og_type":"article","og_title":"Nodejs Global Objects with Examples - DataFlair","og_description":"Learn about Nodejs Global Objects. See built-in global objects with codes and examples for proper understanding.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-12-02T03:30:22+00:00","article_modified_time":"2021-12-02T04:34:57+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-objects.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Nodejs Global Objects with Examples","datePublished":"2021-12-02T03:30:22+00:00","dateModified":"2021-12-02T04:34:57+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/"},"wordCount":437,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-objects.webp","keywords":["Nodejs Global Objects"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/","name":"Nodejs Global Objects with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-objects.webp","datePublished":"2021-12-02T03:30:22+00:00","dateModified":"2021-12-02T04:34:57+00:00","description":"Learn about Nodejs Global Objects. See built-in global objects with codes and examples for proper understanding.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-objects.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-global-objects.webp","width":1200,"height":628,"caption":"nodejs global objects"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-global-objects-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Nodejs Global Objects with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=103452"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103452\/revisions"}],"predecessor-version":[{"id":103998,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103452\/revisions\/103998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/103994"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=103452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=103452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=103452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}