Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What are the global objects of Node.js ?

  • Last Updated : 07 Oct, 2021

Node.js is a JavaScript framework based on an open-source project that is used for server-side scripting. Node.js Global Objects are those objects which are present in all modules. Global Objects can be used directly in the application which is available without importing any module. Global objects have been added keeping in mind that these are some basic requirements and one can require these any time so it is better to keep them global rather than to add specific modules for these objects. Global objects are created while building a framework in the first place and later on can be updated based on the requirements. Global objects are responsible for the basic functionality of the framework without importing any modules or creating any functions in order to meet some of the primary requirements.

Some of the Node.js Global Objects are listed below:  

  1. Buffer Class: The Buffer class is a globally accessible class. The Buffer class is mainly used for the purpose of dealing with binary data. The objects of the Buffer class are often used to represent the binary data in the form of a byte sequence.  

  2. console: It is used to print to stdout& stderr and it is a global object that can be accessed without the import of any module.  

  3.  process: It is an instance of EventEmitter which is used to receive information on the current process. It is an inbuilt global object that and can also be accessed using require() explicitly.  

  4. global: It is a namespace and defining a variable within this namespace makes it a global variable. 

    var myvar;
  5. setImmediate() method: The functions for the callbacks are aligned executed as per the order of their creation. The callback queue processes the callbacks at every iteration of the event loop. It immediately executes the scheduled callbacks. If the executing callback is bound by a timer, the timer will not get triggered until the instantiation of the iteration of the next loop.

    Javascript




    <script>
    function myFunction(website){
        console.log("Hi, Welcome to " + website);
    }
      
    console.log("Before the setImmediate call")
    let setID = setImmediate(myFunction, "GFG");
    console.log("After the setImmediate call")
    for(let i=0; i<5; i++){
        console.log("Iteration of loop: "+i);
    }
    </script>

    Output:

  6. clearImmediate() method: It performs the clear operation on the object returned by the setImmediate() method by stopping the object i.e. the object will not be executed.

    Javascript




    <script>
    function myFunction(website){
        console.log("Hi, Welcome to " + website);
    }
      
    console.log("Before the setImmediate call")
    let setID = setImmediate(myFunction, "GFG");
    console.log("After the setImmediate call")
    for(let i=0; i<5; i++){
        console.log("Iteration of loop: "+i);
    }
    clearImmediate(setID);
    </script>

    Output:

  7. setInterval() method: If the interval exceeds the limit of int data type i.e. 2147483647 or is not a positive integer then the interval value is set to 1. It also truncates the non-integer delays to the nearest integer and performs the callback function at repeated intervals.

    Javascript




    <script>
    function myFunction(website){
        console.log("Hi, Welcome to " + website);
    }
      
    console.log("Before the setInterval call")
    let setID = setInterval(myFunction, 3000, "GFG");
    console.log("After the setInterval call")
    </script>

    Output:

  8. clearInterval() method: It performs the clear operation on the object returned by the setInterval() method by canceling the object i.e, it doesn’t allow setInterval to execute even once.  

    Javascript




    <script>
    function myFunction(website){
        console.log("Hi, Welcome to " + website);
    }
      
    console.log("Before the setInterval call")
    let setID = setInterval(myFunction, 3000, "GFG");
    console.log("After the setInterval call")
      
    clearInterval(setID);
    </script>

    Output:

  9. setTimeout() method: If the delay exceeds the limit of int data type i.e. 2147483647 or is not a positive integer then the delay value is set to 1. It also truncates the non-integer delays to the nearest integer and performs the callback function at repeated intervals but does not guarantee the exact timings of the callbacks.

    Javascript




    <script>
    function greet(msg, name) {  
       console.log(msg + name);
    }
    setTimeout(greet, 1000, "Hi ", "Geek");
    </script>

    Output:

    Hi Geek
  10. clearTimeout() method: The clearTimeout() method is used to cancel the timeoutObj returned by the setTimeout() method so that setTimeout cannot be executed.

     

    Javascript




    <script>
    console.log("setTimeout msg will not be printed")
    function greet(msg, name) {  
       console.log(msg + name);
    }
    let setID = setTimeout(greet, 1000, "Hi ", "Geeks");
      
    clearTimeout(setID);
    </script>

    Output:

    setTimeout msg will not be printed
  11. queueMicrotask() method: This is executed after the callback function exits and iff the execution stack of the JavaScript is empty. This method does not return the control back to the JavaScript and a microtask queue can be processed multiple times of the loop because the event loop keeps calling microtasks in the queue.

  12. URLSearchParams: This is an API that performs the read and writes operations on any URL.  

    Syntax:

     const myURL = new URL('https://www.register.com/?name=nodejs');
     
    //It prints nodejs
    console.log(myURL.searchParams.get('name'));
    
    myURL.searchParams.append('name', 'xyz');
    
    // It prints https://www.register.com/?name=nodejs&name=xyz
    console.log(myURL.href);
  13. WebAssembly: This is a low-level assembly language supported by modern browsers so we can use this to arrange the objects on the browsers.  

    The following variables exist only within the scope of some modules that might appear to be global.  

    • require(id) method: It can return an object of non-specific datatype and is used to import modules into the script.  
    var express = require('express')
    • exports: It can be used in order to export the modules present in the script to any other script.
    • module: It is used to include a particular module using the require() method and this is not local rather local to each module.  
    •  __dirname: __dirname is not a global object and this can be proved by running the following command without adding any scripts as this will throw an error it is verified that __dirname is not a global object. 
    Create a test.js file
    Write the following condole.log statement:  
    console.log("__dirname : "+ __dirname);
    Run the test.js file

    • __filename: __filename is not a global object and this can be proved by running the following command without adding any scripts as this will throw an error it is verified that __filename is not a global object.
    Create a test.js file
    Write the following condole.log statement:  
    console.log("__filename : "+ __filename);
    Run the test.js file


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!