How to add region to collapse and expand code in java script. If any body knows solution, it will be great. thanks in advance.
Asked
Active
Viewed 5.5k times
55
-
2Possible duplicate of [how to implement regions/code collapse in javascript](https://stackoverflow.com/questions/1921628/how-to-implement-regions-code-collapse-in-javascript) – Andrei Dragotoniu Oct 13 '17 at 08:24
-
Regions are usually editor dependent, not a language feature. Some developers argue that the concept of regions are inherently bad, since they promote hiding ugly code, when you ought to just structure your code properly so regions aren't necessary. – Nikolaj Dam Larsen Oct 13 '17 at 08:28
3 Answers
98
It depends only on the IDE.
vscode, for example, added support for code regions in september 2017
https://code.visualstudio.com/updates/v1_17#_folding-regions
For javascript:
//#region
code
//#endregion
or
//region
and
//endregion
The same syntax could work also in different IDEs.

Michele Federici
- 1,755
- 1
- 14
- 17
-
-
3
-
the exact syntax is `// #region custom_region_name`, see curiousBoy's answer that should be the accepted answer – MAMY Sébastien Nov 22 '22 at 13:40
-
@MAMYSébastien it depends on the IDE implementation and the language used. See the link for vscode provided as example in the answer. – Michele Federici Nov 23 '22 at 14:49
35
This works great on Visual Studio 2017 ( I haven't tried with other versions)

curiousBoy
- 6,334
- 5
- 48
- 56
-
4
-
3
-
Still good in 2022 - much better than the braces I was using before! – Matthew Beck Jun 14 '23 at 21:20
4
for older version(vs2015 and below) select your code block => press CTRL + M+H The Code Will be collapsible

Eid Sky
- 49
- 6