20

Is there anything in JavaScript or Visual Studio to detect if the code is used in debug-mode? Something like "#if DEBUG" in C#, but for JavaScript?

Martin
  • 1,283
  • 2
  • 14
  • 28
  • `if (debug)` would work just fine if you define `debug` when in debug mode. It would be up to you to define that as needed. – jfriend00 Mar 12 '14 at 06:42

4 Answers4

11

A bit late, but I needed the same and could not give up until a viable solution.

I have a kind of "main" javascript file, where I have a line like:

Site.DEBUG = false;

Then in the code I can check for this constant. Now I needed to solve that at build time, some automation would set this for me according to project configuration. Here I've found fnr.exe command-line tool for find and replace in files. It's a quite good piece of utility, would be worth to check out anyway. So at this point I've created a folder in the project directory called BuildScripts, I've copied the fnr.exe file into it, and created a batch file like this.

switch_client_debug.bat

REM Params: path to folder, filename, change-DEBUG-from-this, to-this
fnr.exe --cl --dir "%1" --fileMask "%2" --caseSensitive --showEncoding --find "DEBUG = %3" --replace "DEBUG = %4"

Then I defined the corresponding pre-build events at the web project like this:

cd $(ProjectDir)BuildScripts
call switch_client_debug.bat $(ProjectDir)ts site.ts false true

and its pair at Release config:

cd $(ProjectDir)BuildScripts
call switch_client_debug.bat $(ProjectDir)ts site.ts true false

Now everything works like a charm and I can have logging, tracing, special logic for Debug and for Release configuration in Javascript.

Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
  • 1
    As a follow-up, I have to add the comment that I actually don't use this workaround for a long time. Instead there are several better tools like `gulp` and plugins for scenarios similar to these. However, if one doesn't have an `npm` environment set up, it can be still an acceptable option. – Zoltán Tamási Mar 30 '17 at 16:56
6

No.

#if/#endif are preprocessor directives in C# (and other languages) that tells the compiler to conditionally include/exclude a section of code when compiling.

JavaScript is a script language that is not precompiled, and therefore it would not make much sense to have preprocessor directives like these.

Mårten Wikström
  • 11,074
  • 5
  • 47
  • 87
  • 1
    On the other hand, you can set variables for example from a master page or layout page in case of MCV project. So if your js file has a publicly available variable (not in closure) eg MyNamespace{debugmode: true, MyFunction: function(){ if(MyNamespace.debugmode){console.log("debug mode!");}}} if you set the "debugmode" to false, you're in release mode. You can set this variable from an aspx/cshtml file where you can use build directives depending on the debug/release build mode. The drawback is you expose the js variable which can be modified from browser console too by anyone. – Drusantia Aug 22 '16 at 12:39
  • 1
    Some javascript "compilers" (or minifiers, transformers, whatever they are called) may have this feature. See http://stackoverflow.com/a/2935165/1003746 – Qi Fan Jan 06 '17 at 20:38
3

Only for IE there is the conditional compilation:

/*@cc_on
@set @version = @_jscript_version
@if (@_win32)
document.write("You are running 32 bit IE " + @version);
@elif (@win_16)
document.write("You are running 16 bit IE " + @version);
@else @*/
document.write("You are running another browser or an old IE.");
/*@end @*/

nice article here

ale
  • 10,012
  • 5
  • 40
  • 49
0

Late in the party, but here is what I do on Visual Studio

// #region Uncomment when in debug mode
// localStorage.clear();
// #endregion
Joseph Wambura
  • 2,732
  • 2
  • 21
  • 24