Michael Drum
Michael Drum

Reputation: 1239

Is there something similar to C# regions in JavaScript? Visual Studio 2017

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.

Upvotes: 24

Views: 20025

Answers (5)

Shadab Shaikh
Shadab Shaikh

Reputation: 1

You can try this if using razor or asp web forms, just replace @ with <% and end with %>.

enter image description here

Upvotes: 0

Ban_Midou
Ban_Midou

Reputation: 23

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)

Upvotes: 0

Robert
Robert

Reputation: 686

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.

Upvotes: 32

curiousBoy
curiousBoy

Reputation: 6834

The link in the given answer is broken. Here is a great plugin which I use daily and no issue so far:

Java Script Regions

PS: Not tried this with other than VS 2017

enter image description here

Upvotes: 16

Michael Drum
Michael Drum

Reputation: 1239

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/

Upvotes: 13

Related Questions