I am doing work for somebody who would like their JavaScript library organized into categories: CRUD functions, Form functions, etc. They really like the utility of regions in C# and are asking for this in their JavaScript file. Any suggestions? I am using Visual Studio 2017 and Team Foundation Server.
-
1Regions are a feature of the VIsual Studio IDE not of C# – Steve Sep 17 '17 at 19:16
-
@keiwan i am pretty surr the later versions of VS have no support for macros. I personally think regions should never be used. Organize your code using namespaces and classes etc. – CodingYoshi Sep 17 '17 at 19:20
-
@CodingYoshi I haven't used VS in a while but based on [this answer](https://stackoverflow.com/a/44511194/6214222) there seems to be a plugin that does what the OP wants. – Keiwan Sep 17 '17 at 19:25
-
@Keiwan Thank you for your help. I found a solution, see answer... – Michael Drum Sep 17 '17 at 19:27
-
1@CodingYoshi Thank you for your help. I'm not a big fan of regions either. However, that's what the boss wants. – Michael Drum Sep 17 '17 at 19:27
5 Answers
For VS 2019, Microsoft has fully implemented regions for Javascript, they work exactly they way they work in C#, only, you have to preface the beginning and end of the regions as comments, for example:
//#region My Region
function foo(){
*//code*
}
//#endregion
For anything prior to VS 2019, See Michael Drum's answer. His shortcut also works in VS 2019.

- 624
- 2
- 8
- 19
-
I didn't know that. But it does work me on VS2019. Thanks @Robert. – Mukesh Kumar Jan 27 '20 at 15:53
-
2This (`// #region foobar` and `// #endregion`) works also in Visual Studio Code – KargWare Feb 10 '21 at 00:59
The link in the given answer is broken. Here is a great plugin which I use daily and no issue so far:
PS: Not tried this with other than VS 2017

- 6,334
- 5
- 48
- 56
-
1It is built natively in VS 2019! Follow the same outlining as this plugin – Linju Jul 16 '21 at 19:32
Select the code you want to compress. Press Ctrl + M + H. That code will now be collapsible. For readability, comment above the "region" to give it a name.
Source: http://blog.degree.no/2013/05/define-a-region-in-javascript-files-visual-studio-2010-2012/

- 1,151
- 5
- 14
- 26
-
-
1I manage to get the saved copy from google: http://webcache.googleusercontent.com/search?q=cache:http://blog.degree.no/2013/05/define-a-region-in-javascript-files-visual-studio-2010-2012/ – dawncode Apr 25 '18 at 19:42
-
1Google's link is down. Here is an archived link for those interested: https://web.archive.org/web/20180321040414/http://blog.degree.no/2013/05/define-a-region-in-javascript-files-visual-studio-2010-2012 – Lukas Jul 01 '20 at 14:38
a Lot of people have already answered on how to get this in Visual studio I will however suggest a different approach. Since #region is a tag limited to visual studio, it will mostly not be detected by other editors.
But in most other editors, they do however recognize "if" and "while" conditions. You can use if (1) and While(1) to create blocks of collapsible code that can be folded.
for eg
// This is a comment for the code block below
if(1){
....
...
}
you will get a -/+ sign next to this code block in many others editors (Unless you are using basic notepad)

- 25
- 4