In JavaScript, global object is a built-in object that provides access to the global scope and its associated properties, methods, and objects. The global object is always available, and its properties and methods can be accessed from anywhere in the JavaScript code.
The global object serves as the top-level container for all global variables, functions, and objects. The specific representation of the global object depends on the execution environment:
- In a browser, the global object is window.
- In Node.js, the global object is global.
- In strict use mode, it will be undefined.
When you declare a variable or function in the global scope (outside of any function or block), it becomes a property of the global object.
Whenever Global is used, it may be either window or global.
Key Characteristics of the Global Object
Global Scope: Any variable or function declared in the global scope is a property of the global object.
Example (in the browser):
var myGlobalVar = 'Hello, world!';
console.log(window.myGlobalVar); // "Hello, world!"
Accessing Global Object:
- In a browser, the global object is window.
- In Node.js, the global object is global.
In ECMAScript modules, the global object can be accessed via globalThis.
Common Methods and Properties of the Global Object
Methods:
- setTimeout() and setInterval()
- clearTimeout() and clearInterval()
- parseInt() and parseFloat()
- isNaN()
- decodeURIComponent() and encodeURIComponent()
- eval()
Properties:
- Infinity
- NaN
- undefined
setTimeout() and setInterval():
- Used to schedule code to run after a delay or at regular intervals.
- setTimeout() runs a function after a specified delay (in milliseconds).
- setInterval() runs a function at regular intervals (in milliseconds).
Example:
setTimeout(() => console.log("Hello after 2 seconds"), 2000);
setInterval(() => console.log("Hello every 2 seconds"), 2000);
clearTimeout() and clearInterval(): Used to cancel a timeout or interval previously set with setTimeout() or setInterval().
Example:
const timerId = setTimeout(() => console.log("This won't run"), 2000);
clearTimeout(timerId);
const intervalId = setInterval(() => console.log("This won't run"), 2000);
clearInterval(intervalId);
parseInt() and parseFloat(): These methods convert a string into an integer (parseInt()) or a floating-point number (parseFloat()).
Example:
let num1 = parseInt('123px'); // 123
let num2 = parseFloat('123.45px'); // 123.45
isNaN(): Checks whether a value is NaN (Not a Number).
Example:
isNaN('Hello'); // true
isNaN(123); // false
decodeURIComponent() and encodeURIComponent(): Used to encode and decode URI components, such as query parameters.
Example:
let encoded = encodeURIComponent('Hello, world!');
console.log(encoded); // "Hello%2C%20world%21"
let decoded = decodeURIComponent(encoded);
console.log(decoded); // "Hello, world!"
eval():
- Executes a string of JavaScript code as if it were part of the script.
- Use with caution due to security risks and performance issues (e.g., code injection).
Example:
let result = eval('2 + 2');
console.log(result); // 4
globalThis:
- A standard reference to the global object in any JavaScript environment (whether in the browser, Node.js, or elsewhere).
- Provides a consistent way to access the global object across different environments.
Example:
console.log(globalThis); // In the browser, it logs the `window` object.
Additional Properties of the Global Object
Infinity: Represents positive infinity.
console.log(Infinity); // Infinity
NaN: Represents "Not-a-Number" (a special numeric value).
console.log(NaN); // NaN
undefined: Represents an uninitialized or undefined variable.
let x;
console.log(x); // undefined
Object, Function, Array, etc.: Global constructors for various JavaScript objects (such as Object, Function, Array, String, etc.).
let obj = new Object(); // Creates a new empty object
let arr = new Array(1, 2, 3); // Creates a new array
console: Provides access to logging functions like console.log(), console.error(), etc., for debugging purposes.
console.log('Hello, world!');
Conclusion
- The global object provides access to global properties and methods in JavaScript.
- In browsers, the global object is the window object, while in Node.js, it is global.
- Methods like setTimeout(), setInterval(), parseInt(), and isNaN() are part of the global object, offering utility functions for various tasks.
- For modern JavaScript development, globalThis offers a consistent way to access the global object across different environments.
No comments:
Post a Comment