What happens when a function declaration and a var variable are hoisted in the same scope?
- The var variable takes precedence and shadows the function declaration.
- The function declaration takes precedence and shadows the var variable.
- JavaScript throws an error since function declarations and var variables can't be hoisted in the same scope.
- They both coexist in the same scope, and there's no shadowing or precedence.
When a function declaration and a var variable with the same name are hoisted in the same scope, the function declaration takes precedence and shadows the var variable. This is known as "hoisting," and it means that the function is accessible throughout the scope, even before its actual declaration in the code.
Loading...
Related Quiz
- While developing a web application, you create a class "Product" to hold the basic attributes of products in an e-commerce site. Later, you need to extend the functionality of some products which are on sale without altering the "Product" class. Which design pattern might be most appropriate to implement this additional functionality without modifying the existing "Product" class?
- When using a for...of loop with strings, each iteration will provide a single _______.
- What is the primary purpose of classes in JavaScript?
- While working on a project, you found out that the API you are fetching data from sends relevant error messages in the body of the response, even when the request fails. How do you extract and use this error message in JavaScript?
- How does the pop() method behave when applied on an empty array?