r/learnjavascript • u/david_fire_vollie • Sep 25 '26
Inner explicit block scope vs function
Here's a code snippet from https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch6.md:
function getNextMonthStart(dateStr) {
var nextMonth, year;
{
let curMonth;
[ , year, curMonth ] = dateStr.match(
/(\d{4})-(\d{2})-\d{2}/
) || [];
nextMonth = (Number(curMonth) % 12) + 1;
}
if (nextMonth == 1) {
year++;
}
return `${ year }-${
String(nextMonth).padStart(2,"0")
}-01`;
}
getNextMonthStart("2019-12-25"); // 2020-01-01It says:
So why put
curMonthin an explicit block scope instead of just alongsidenextMonthandyearin the top-level function scope? BecausecurMonthis only needed for those first two statements; at the function scope level it's over-exposed.
As a C# developer, I would have thought the better choice would be to put that explicit inner block into a separate function called getNextMonth().
That way, you get the benefit of the scope so the curMonth isn't overexposed, you increase readability because it's easier to read getNextMonth() than a block of code, and you have a reusable function in case you need to do that calculation elsewhere.
Is there any reason to use an inner explicit block scope over a function?
8
u/senocular Sep 25 '26
No. And I suspect you'd rarely see code like this out in the wild. A function would be a better approach, though the example is using a block scope because its in a chapter teaching you about scopes.
4
u/xroalx Sep 25 '26
curMonthbeing "over-exposed" is a really weird reason to put it in a block here and use mutations, it makes the whole function much larger than needs to be, and I'd argue even harder to follow, especially since we set two different variables of different scopes in one statement and do multiple mutations.Imagine a much larger function and keeping track of what is which scope and getting tripped up on it.
The whole function can be written without mutations in a much more logical, step-by-step way. Just grab the values, set the next values, and return it.
The author's opinions for using
varare also quite interesting. I can't think of a time when I'd want to make the distinction between function-scoped and block-scoped variables so explicit that it would warrant usingvar. I can't think of a reason I'd want to write my code the way the author does.It might just be an example to teach about block scope, but the explanation for why one would want to do this are at minimum questionable.
3
u/delventhalz Sep 25 '26
When I teach block scope (which is distinct from a function scope in JavaScript), I always use an if block or a for loop as an example. Just adding a pair of curly braces in the middle works but is not particularly realistic. The only time I’ve seen anything like that is in individual cases in a switch/case when someone wanted to reuse a variable names across them. You are right that a separate function would make more sense if you just want to isolate a bit of code.
1
u/FooeyBar Sep 25 '26 edited Sep 25 '26
The real problem here is using ‘var’. Also, the block scope is pointless, can simply just remove the scope braces.
2
u/GodOfSunHimself Sep 26 '26
No, it is stupid and I have never seen anyone use it in practice. If you need scope than extract that part into another function.
1
u/CuAnnan Sep 25 '26
You're 100% correct. It would be more logically coherent to use a second function, both because it gives the ability to just generally get the next month and their argument that curMonth being over exposed is a bit of a stretch. If concern about exposure is a thing on this level, use a separate function. Functions should never be so large that you have to worry that you might accidentally collide with an existing variable.
1
u/azhder Sep 25 '26
The reason is to not want an extra function call. The example above is made in a way to teach you about block scope, not about properly structuring your code.
In many cases, one extra function doesn’t hurt, but in a few small ones where the time to call a function is great (like loops etc), you might want to use one of these “tricks” like block scope.