1

In the example below, I can call the functional expression b from the functional expression a even though b() is not defined at that point. Could you explain why this works?
Thanks in advance!

function go() {
  const a = () => {
    b();
  }
  const b = () => {
    console.log("abc")
  }
  a();
}
go();
SpoKaPh
  • 167
  • 2
  • 12

1 Answers1

3

It has been defined by the time the a function runs. a() is below const b =, so b has been defined.

But it wouldn't work if b was defined after

function go() {
  const a = () => {
    b();
  }
  a();
  const b = () => {
    console.log("abc")
  }
}
go();

It's OK for a function body to reference something that hasn't been defined yet. But it's not OK for that function to run before the variable has been defined.

Snow
  • 3,820
  • 3
  • 13
  • 39
  • So the answer was in the question. The question was wrong. They are not calling a function before declaring/defining it. – bitoolean Oct 23 '20 at 01:46