S
S
SimpleShadow2015-08-14 22:05:00
JavaScript
SimpleShadow, 2015-08-14 22:05:00

How to properly debug JS?

Hello. I have been working with JS for a long time, but I never learned how to debug it correctly. How can I find out where the error occurred? If everything is simple with php - it displays errors on the screen and writes them to the logs, then what about JS. If I need to see if a variable has the correct value, then every time I print it to the console using console.error? And if the DOM element is incorrectly specified, how do you know?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2015-08-14
@alexey-m-ukolov

How can I find out where the error occurred?
console.error displays the complete call stack with links to specific lines of specific scripts.
It is also very convenient to turn on the "Pause on exceptions" mode, in which the debugger is automatically turned on when an error occurs. But for this, Developer Tools must be open (although if you are a web developer, then in principle they never close).
Well, all other problems are easily solved with the help of a debugger. There you can see the values ​​​​of variables and the call stack and logic step by step.

D
Denis Ineshin, 2015-08-14
@IonDen

Chrome Developer Tools have all the necessary tools for debugging.
If you want to stop code execution in a specific place and look at the entire stack, then the easiest way is to use the debugger keyword:

function foo (bar) {
    var a = 1;
    // some code...
    debugger;
    // some code...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question