Hoisting and Execution Context
What will be the output of this code?
function example() {
console.log(this);
}
example();
The output will be the global object (window in browsers, global in Node.js). In JavaScript, when a function is called as a standalone function (not as a method of an object, not with `new`, and not with `.call`/`.apply`/`.bind`), the value of `this` inside the function defaults to the global object. This is true in non-strict mode. However, it's important to note that if strict mode is enabled (`'use strict';`), the behavior would be different - `this` would be `undefined` instead of the global object: ```javascript 'use strict'; function example() { console.log(this); // undefined } example(); ``` This behavior of `this` is one of the most confusing aspects of JavaScript for many developers. The value of `this` is not determined by where a function is defined (unlike lexical scope), but by how it is called. This is known as the 'runtime binding' of `this`. Understanding the different ways `this` can be bound is crucial for effective JavaScript programming, especially when working with object-oriented patterns or event handlers.