Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify code example by using a control structure with the block #1894

Open
dariasavinova opened this issue Jan 29, 2025 · 2 comments
Open

Comments

@dariasavinova
Copy link

Please type "I already searched for this issue":
I already searched for this issue

Edition: (1st or 2nd)
2nd

Book Title:
You Don't Know JS Yet: Scope & Closures

Chapter:
Chapter 1: What's the Scope?

Section Title:
Hoisting

Question:
I’d like to suggest a small adjustment to the code example with the block {} inside the function. While it’s valid JS, using a block like this without a control structure (e.g., if, for, etc.) may be confusing for some readers.

It could be clearer if we used a real control structure, like if (true) { ... }. This would make it more obvious that the block is just creating a new scope, while also demonstrating a more typical use of blocks in JS.

Thanks for considering this!

@getify
Copy link
Owner

getify commented Jan 29, 2025

I understand what you're asking, but... I think if (true) { .. } is likely to cause even more "WTF?" in the average reader.

@dariasavinova
Copy link
Author

Thanks for your response!
Yeah, if (true) { .. } is a bit amusing - I totally see how it might cause that kind of reaction. But I noticed that Get Started already includes similar examples, like:

var adult = true;

if (adult) { .. }

Since if (adult) essentially behaves like if (true), maybe we could use something similar here as well? That way, it would still feel natural while keeping the explanation clear.
In Get Started, it looked pretty clean and intuitive. A standalone {} without any control structure, on the other hand, feels a bit unusual in practice, so I was just thinking about making it blend more seamlessly.

Do you think there’s a way to keep it intuitive for readers while staying true to the explanation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants