๐—Ÿ๐—ฒ๐˜'๐˜€ ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ฟ๐˜€๐˜๐—ฎ๐—ป๐—ฑ ๐—ฎ๐—ฏ๐—ผ๐˜‚๐˜ ๐—”๐—ฐ๐—ฐ๐—ถ๐—ฑ๐—ฒ๐—ป๐˜๐—ฎ๐—น ๐—š๐—น๐—ผ๐—ฏ๐—ฎ๐—น ๐—ฉ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜

ยท

1 min read

๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ณ๐—ฟ๐—ถ๐—ฑ๐—ฎ๐˜†() { ๐—น๐—ฒ๐˜ ๐—ฎ = ๐—ฏ = ๐Ÿฌ; }

๐—ณ๐—ฟ๐—ถ๐—ฑ๐—ฎ๐˜†();

console.log("a: ", a); // ๐—ฅ๐—ฒ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ๐—˜๐—ฟ๐—ฟ๐—ผ๐—ฟ: ๐—ฎ ๐—ถ๐˜€ ๐—ป๐—ผ๐˜ ๐—ฑ๐—ฒ๐—ณ๐—ถ๐—ป๐—ฒ๐—ฑ console.log("b: ", b); // ๐—ฏ: ๐Ÿฌ

๐—ฆ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—บ๐—ถ๐—ด๐—ต๐˜ ๐—ฏ๐—ฒ ๐˜๐—ต๐—ถ๐—ป๐—ธ๐—ถ๐—ป๐—ด ๐˜„๐—ต๐˜† ๐—ผ๐—ป ๐—ฐ๐—ผ๐—ป๐˜€๐—ผ๐—น๐—ฒ.๐—น๐—ผ๐—ด("๐—ฏ: ", ๐—ฏ) ๐—ถ๐˜ ๐—ถ๐˜€ ๐—ด๐—ถ๐˜ƒ๐—ถ๐—ป๐—ด "๐—ฏ: ๐Ÿฌ" ๐—ถ๐—ป๐˜€๐˜๐—ฒ๐—ฎ๐—ฑ ๐—ผ๐—ณ ๐—ฎ ๐—ฅ๐—ฒ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ๐—˜๐—ฟ๐—ฟ๐—ผ๐—ฟโ“

Let's see

This statement inside function friday()

let a = b = 0; can be break down into two statements, i.e.

b = 0 // global scope let a = b // friday() scope

๐—›๐—ฒ๐—ป๐—ฐ๐—ฒ, ๐—ฎ ๐—ถ๐˜€ ๐—ป๐—ผ๐˜ ๐—ฎ๐˜ƒ๐—ฎ๐—ถ๐—น๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ถ๐—ป ๐—ด๐—น๐—ผ๐—ฏ๐—ฎ๐—น ๐˜€๐—ฐ๐—ผ๐—ฝ๐—ฒ ๐˜„๐—ต๐—ถ๐—น๐—ฒ ๐—ฏ ๐—ถ๐˜€ ๐—ฎ๐˜ƒ๐—ฎ๐—ถ๐—น๐—ฎ๐—ฏ๐—น๐—ฒ.

That's why on printing a and b in global scope b is printed but a says that it is not defined. ๐—œ๐—ป ๐—ผ๐˜๐—ต๐—ฒ๐—ฟ ๐˜„๐—ผ๐—ฟ๐—ฑ๐˜€, ๐—ฏ ๐—ถ๐˜€ ๐—ฎ ๐—ด๐—น๐—ผ๐—ฏ๐—ฎ๐—น ๐˜ƒ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ฐ๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ๐—ฑ ๐—ฎ๐—ฐ๐—ฐ๐—ถ๐—ฑ๐—ฒ๐—ป๐˜๐—ฎ๐—น๐—น๐˜†.

To ๐—ฝ๐—ฟ๐—ฒ๐˜ƒ๐—ฒ๐—ป๐˜ your code from creating global variables accidentally, you can ๐˜‚๐˜€๐—ฒ ๐˜€๐˜๐—ฟ๐—ถ๐—ฐ๐˜ mode.

carbon (5).png

ย