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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *