AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js Global Object


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js Global Object

What is the global object in nodeJS?

Node.js global objects are global in nature and are available in all modules.
We do not need to include these objects in our application, rather we can use them directly.
These objects are modules such as: arrays, functions, strings and object itself as explained below..

In this chapter we study the following objects:
__filename
__dirname
setTimeout(cb, ms)
clearTimeout(t)
setInterval(cb, ms)

 

Node.js __filename object

__filename indicates the name of the running script file.
The absolute path to the location of the output file, and the command line parameter and specify the file name could be different.
If it is in the module, the returned value is the path to the module file..

Create a js file named my_test_main.js with the following code −

Example: Create a file main.js, code as:
// Let's try to print the value of __filename

console.log( __filename );

Output:
$node main.js
/home/files_script/my_test_main.js

Related subjects:
node.js http module node.js modules Install nodejs

 

Tags: nodejs global: variable across file, object, variable, error handler, function, typescript, scope, exception handler, await, node js globalthis
nodejs global object documentation, set global object, request object What is nodeJS global?
How do I declare global in node JS?
Which of the following are global objects in Node JS?
What is the global object in nodeJS?
What is a global object?
What is __ Dirname global object in nodeJS?
How do I declare a global variable in node JS?

 

Node js __dirname object

Node js __dirname object represents the name of the directory where currently executing script resides in.

Create a js file named my_test_main.js with the following code:

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

console.log( __dirname );

Output:
$node main.js
/home/files_script

 

Nodejs setTimeout(cb, ms)

The setTimeout(cb, ms) global function is used to cb (run callback) after the least ms (millisecond).

The delay may depend on like OS timer granularity and system load.

SetTimeout () function is specified only once and
It returns a handle value represents the timer.

Example: Create a js file named my_test_main.js with the following code −:
function printMyscript() {
console.log( "Hi, This is my script node js");
}

// Now call above function after 4 seconds
setTimeout(printMyscript, 4000);

Output:
$node main.js
Hi, This is my script node js

 

clearTimeout (t)

clearTimeout (t) is used to stop a global function before passing setTimeout () to create a timer.

Example: Create a js file named my_test_main.js with the following code −:
function printMyscript() {
console.log( "Hi, This is my script node js");
}

// Now call above function after 4 seconds
var t = setTimeout(printMyscript, 4000);

// Now clear the timer
clearTimeout(t);

 

setInterval(cb, ms)

The setInterval(cb, ms) global function is used to run callback cb repeatedly after at least ms milliseconds.

Example: Create a js file named my_test_main.js with the following code −:
function printMyscript() {
console.log( "Hi, This is my script node js");
}

// Now call above function after 4 seconds
setInterval(printMyscript, 4000);
Note: The above program will execute printMyscript() after every 4 second.

 



nodejs global: variable across file, object, variable, error handler, function, typescript, scope, exception handler, await, node js globalthis
nodejs global object documentation, set global object, request object What is nodeJS global?
How do I declare global in node JS?
Which of the following are global objects in Node JS?
What is the global object in nodeJS?
What is a global object?
What is __ Dirname global object in nodeJS?
How do I declare a global variable in node JS?
Node.js Global Object - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...