clear

Clears all user-defined global variables.

Methods clear()

Purpose: This method clears all user-defined global variables from the Node.js global object while preserving essential global variables like global and process. It provides a way to reset or clean up global state within your application.

Parameters: None.

Returns: None.

Usage Example:

const globalManager = require('global-manager');

// Set global variables
globalManager.set('key1', 'value1');
globalManager.set('key2', 'value2');

// Clear all user-defined global variables
globalManager.clear();

// Check if variables have been cleared
console.log(globalManager.has('key1')); // Output: false
console.log(globalManager.has('key2')); // Output: false

Detailed Explanation: The clear method removes all user-defined global variables that have been set using the set method. It is useful when you want to reset the global state of your application or clean up global variables that are no longer needed. This method ensures that only user-defined variables are removed, leaving essential Node.js global variables intact.

Notes:

  • Essential global variables like global, process, and other built-in Node.js globals are not affected by clear.

  • After calling clear, attempting to retrieve any previously set global variables using get will return undefined.

Last updated