24

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?

1
  • 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. Commented Mar 12, 2014 at 6:42

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

2 Comments

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.
Some javascript "compilers" (or minifiers, transformers, whatever they are called) may have this feature. See stackoverflow.com/a/2935165/1003746
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

1 Comment

Thank you, but I would need a cross-browser-solution.
0

Even later but I was looking for something like this today.

Assuming your conditional statements fit on one line (like console.log for example), the following works OK:

Start by prefixing all debug lines with "//#IFDEBUG" e.g. //#IFDEBUG console.log('show debug message here');

Any comment lines like this will be stripped out by build / compression tools and don't do anything.

When you want to debug, perform a search-and-replace on "//#IFDEBUG " and replace it with "/*@IFDEBUG*/ "

The debug lines will now be used when running the script.

When you've finished using your debug lines, reverse this and replace "/*#IFDEBUG*/ " with "//#IFDEBUG "

Comments

-1

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

// #region Uncomment when in debug mode
// localStorage.clear();
// #endregion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.