From 2a0f311937d537f9dc509002f53e12267ad431ab Mon Sep 17 00:00:00 2001 From: Andrei Date: Sat, 16 Nov 2024 04:27:03 +0200 Subject: [PATCH] Update solution.md for task "Function in if" --- .../03-closure/5-function-in-if/solution.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/1-js/06-advanced-functions/03-closure/5-function-in-if/solution.md b/1-js/06-advanced-functions/03-closure/5-function-in-if/solution.md index e2e7a91b33..7e8a698ead 100644 --- a/1-js/06-advanced-functions/03-closure/5-function-in-if/solution.md +++ b/1-js/06-advanced-functions/03-closure/5-function-in-if/solution.md @@ -1,3 +1,10 @@ -The result is **an error**. +With `'use strict'`: -The function `sayHi` is declared inside the `if`, so it only lives inside it. There is no `sayHi` outside. \ No newline at end of file +An error occurs: `ReferenceError: sayHi is not defined`. +The function `sayHi` is declared inside the block (`if`), so it only exists within that block. +In strict mode, functions declared inside a block have block-level scope, similar to variables declared with let or const. + +Without `'use strict'`: + +The function `sayHi` is accessible outside the block. This is because, in non-strict mode (and older JavaScript specifications before ES6), functions declared inside blocks like `if`, `for`, etc., +are hoisted to the nearest function or global scope, making them accessible outside the block.