We now have a youtube channel. Subscribe!

Node.js | Global Objects

Node.js Global Objects


Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js Global Objects.

Node.js global objects are global in nature and they are present in every modules. We do not need to include these objects in our application, rather we can use them directly. These global object are functions, modules, strings, and object itself as explained below -

_filename

It represents the filename of the code being executed. This is the resolved absolute path of this code file. The value inside a module is the path to that module file.

Example

Create a js file, main.js using the following code -

// Let's try to print the value of __filename

console.log( __filename );

Now run the main.js to see the result -

$ node main.js

Output

Based on the location of your program, it'll print the main file name as follows -

/web/com/1427091028_21099/main.js

_dirname

This represents the name of the directory that the currently executing script dwells in.

Example

Create a js file, main.js using the following code -

// Let's try to print the value of __dirname

console.log( __dirname );

Now run the main.js to see the result -

$ node main.js

Output

Based on the location of your program, it'll print the current directory name as follows -

/web/com/1427091028_21099


setTimeout(cb, ms)

The setTimeout(cb, ms) global function is used to run a callback cb after at least ms milliseconds. The actual delay depends on external factors such as OS timer granularity and system load. A timer cannot span more than 24.8 days.

Return Value

This function returns an opaque value that represents the timer which can be used to clear the timer.

Example

Create a js file, main.js using the following code -

function printHello() {
   console.log( "Hello, World!");
}

// Now call above function after 2 seconds
setTimeout(printHello, 2000);

Now run the main.js to see the result -

$ node main.js

Output

Verify the output.

Hello, World!

clearTimeout(t)

The clearTimeout(t) global function is used to stop a timer that was previously created with the setTimeout() function. Here, t is the timer returned by the setTimeout() function.

Example

Create a js file, main.js using the following code -

function printHello() {
   console.log( "Hello, World!");
}

// Now call above function after 2 seconds
var t = setTimeout(printHello, 2000);

// Now clear the timer
clearTimeout(t);

Now run the main.js to see the result -

$ node main.js

Output

Verify the output and nothing is going to be printed.


setInterval(cb, ms)

serInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms milliseconds. The actual delay depends on external factors like Os timer granularity and system load. A timer cannot span more than 24.8 days.

Return Value

This function returns an opaque value that represents the timer which can be used to clear the timer via clearInterval() function.

Example

Create a js file, main.js using the following code -

function printHello() {
   console.log( "Hello, World!");
}

// Now call above function after 2 seconds
setInterval(printHello, 5000);

Now run the main.js to see the result -

$ node main.js

Output

The above program will execute printHello() after every 5 seconds.

Global Objects

The following table below provides a list of other objects that are frequently used in our applications. For a more details, kindly refer to the official documentation.

Sr.No.Module Name & Description
1Console

Used to print information on stdout and stderr.

2Process

Used to get information on current process. Provides multiple events related to process activities.



Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js Console Module.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain