We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

back to the lesson

Function in if

importance: 5

Look at the code. What will be the result of the call at the last line?

let phrase = "Hello"; if (true) { let user = "John"; function sayHi() { alert(`${phrase}, ${user}`); } } sayHi();

The result is an error.

The function sayHi is declared inside the if, so it only lives inside it. There is no sayHi outside.

close