Search code examples
javascriptvisual-studio-2022visual-studio-debugging

How can I stop the debugger from closing immediately? (JS in VS2022)


I'm learning Javascript and using Visual Studio 2022 to run my program. I've used VS2022 for C++, but never for Javascript. Unlike for C++, the debugger for JS closes immediately after finishing. How can I prevent that from happening? I need to be able to see my console.log() statements being printed.

I've been putting a console.log() statement at the end with a breakpoint, which worked, but I'm hoping there's a cleaner solution.


Solution

  • Use debugger; statement: Insert the debugger; statement in your code where you want to break. When the debugger encounters this statement, it will pause execution, and you can inspect the output in the console.

    console.log("Hello, World!");
    debugger; // Execution will pause here

    And you can also use Visual Studio Code.