-4

My website contains many sections. I tried so far to put variables and functions for each sections in block scope. Why isn't this programming style used anywhere? Why does no one use global block scopes in the main JavaScript file?

What I mean:

// Global Functions //

  ...

// End Global Functions //

// Scroll Scope //
{
   ...
}
// End Scroll Scope //

// Change Theme Scope //
{
   ...
}
// End Toggle Menu Scope //

// Filter Gallery Scope //
{
  ...
}
// End Filter Scope //
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Philo
  • 428
  • 4
  • 11
  • 1
    In my opinion this question should not have been closed. It could be that there is a practical implication of using scopes in JS, e.g. in regards to performance or maintainability. So this is not (only) about opinion. – code-gorilla May 30 '22 at 19:52
  • 1
    [It is sometimes used](https://stackoverflow.com/q/33534485/1048572) if you just want to write a small script and use variables without polluting the global scope. However, for anything serious you'd just use ES6 modules right away. – Bergi Jun 06 '22 at 04:05
  • @code-gorilla, while there might be truth in your comment, as currently formulated, the question definitely is not a good fit. It literally asks "_Why does no one use (...)_" and that question cannot be answered with facts. If the question would be updated with some research and a more factual question, it might get reopened. See the [tour](https://stackoverflow.com/tour) and [ask]. – wovano Jun 08 '22 at 05:13
  • By the way, I now see that you already added an answer that starts with "_I think this style is not used because..._", which kinda proves that the question leads to opinion-based answers. – wovano Jun 08 '22 at 05:16

1 Answers1

-2

I think this style is not used, because the best practice is to split up code into small functions/classes/methods anyway. This way, the variable count in each scope is reduced without using scoping blocks.

Unlike other languages, like Rust, where scoping has immediate consequences on memory allocation, I don't see a huge benefit in JavaScript to declare scopes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
code-gorilla
  • 2,231
  • 1
  • 6
  • 21