How does the ‘this’ keyword behave differently when used inside a function declaration versus a function expression?
- 'this' refers to the global object (e.g., window in a browser) when used inside a function declaration.
- 'this' refers to the local context where the function expression is defined, allowing for more precise control over its behavior.
- 'this' always refers to the function itself, regardless of whether it's a declaration or expression.
- 'this' behaves the same way in both function declarations and function expressions.
When 'this' is used inside a function declaration, it typically refers to the global object (e.g., 'window' in a browser), which can lead to unexpected behavior in some cases. In contrast, 'this' inside a function expression refers to the local context where the function is defined, making it more predictable and controllable.
Loading...
Related Quiz
- Which of the following is NOT a type of polymorphism supported by JavaScript?
- To merge two arrays into a single array, you can use the _______ method.
- Which method would you use to replace an HTML element with another?
- The concept of creating a single function or method that can operate on multiple types of objects is known as _________.
- What does the "this" keyword refer to in JavaScript?