Global Objects
Node.js global objects are global in nature and it is available in all modules. You don't require to include these objects in your application, in fact, they can be used directly. These objects are modules, functions, strings, and object etc.
A list of Node.js global objects are given below:
- filename
- dirname
- setImmediate(callback[, arg][, ...])
- setInterval(callback, delay[, arg][, ...])
- setTimeout(callback, delay[, arg][, ...])
- clearImmediate(immediateObject)
- clearInterval(intervalObject)
- clearTimeout(timeoutObject)
- process
- console
js _filename
It is a string. It specifies the name of the directory
File: app.js
Output:
js _dirname: The __dirnamerepresents the name of the directory that the currently executing script resides in.
Output:
setImmediate(callback[, arg][, ...])
The setTimeout(cb, ms) global function is used to run callback cb after at least milliseconds. The actual delay in this function depends on external factors like OS timer granularity and system load. A timer cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the timer.
Example
Create a js file named app.js with the following code –
Run the code:
clearTimeout(t)global function is used to stop a timer that was previously created with setTimeout(). Here t is the timer returned by the setTimeout() function.
Example
Create a js file named app.js with the following code –
Run the above code:
setInterval(cb, ms)
The setInterval(cb, ms) global function is used to run callback repeatedly after at least milliseconds. The actual delay depends on external factors like OS timer granularity and system load. A timer cannot span more than 24.8 days.
This function returns an opaque value that represents the timer which can be used to clear the timer using the function
clearInterval(t).
Example
Create a js file named main.js with the following code –
Run the above code
The above program will execute “Hello, World!”