"if debug" in JavaScript?

14,016

Solution 1

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.

Solution 2

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.

Solution 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

Share:
14,016

Related videos on Youtube

Martin
Author by

Martin

Updated on June 04, 2022

Comments

  • Martin
    Martin almost 2 years

    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
    Martin about 10 years
    Thank you, but I would need a cross-browser-solution.
  • Drusantia
    Drusantia over 7 years
    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.
  • Qi Fan
    Qi Fan over 7 years
    Some javascript "compilers" (or minifiers, transformers, whatever they are called) may have this feature. See stackoverflow.com/a/2935165/1003746
  • Zoltán Tamási
    Zoltán Tamási about 7 years
    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.