1

function test1(a = 1) {
  console.log(a);
  var a = 2;
  function a() {}
}
function test2(a = 1) {
  console.log(a);
  function a() {}
}
test1(); // console log  1
test2(); // console log  [Function: a]

According to the understanding of variable hoisting, the variable a in the first function has the same name as the function a, and finally the function a covers the variable a, and there is only the function a in the second function. The output result should be the same.So Why is the function default parameter valid in the test1 function but not in the test2 function?

Drenai
  • 11,315
  • 9
  • 48
  • 82
octkun
  • 91
  • 1
  • 6
  • 2
    I get your output with Chrome/Edge. Firefox logs `function a() { }` twice. – Andreas Mar 18 '22 at 09:03
  • u declare variable **a** in the first function which means ur first function will be **equal to 2** and its not looking at ur parameter, and by the way ur parameters are **optional** so when put only **a** without **= 1** the result will ur a function. i wouldn't recommend using the variables in this way – mmh4all Mar 18 '22 at 09:10
  • 2
    While it can be interesting to delve into the details here… this is all irrelevant for all practical intents and purposes, as you'd never write code like this and expect it to behave in some certain way, because it's all contradictory from the start… – deceze Mar 18 '22 at 09:17

0 Answers0