What are the rights to javascript debugging?

I made a function called test() in a javascript file. Add a simple alert to it.

In an html file called a button click method. But he was not called.

The problem was in the 11th function, nowhere related to mine !!!! But how can a person creating his first javascript function find this?

I am looking for the best ways to debug javascript.

+4
source share
4 answers

You can debug javascript using many modern browsers. See this question for details on how to debug in Google Chrome:

How to run JavaScript debugger in Google Chrome?

In addition, you should not use alert() for debugging, as this can give different production version results due to alert() causing a pause in the script.

It is best to use console.log() and view the output in the browser console.

You can also put debugger in your javascript code to force a breakpoint. However, I prefer not to use this because I forget to remove it before the deployment pauses the script, which can be pretty awkward!

+5
source

You must use the debug console provided by the browser.

Chrome is built into it, press CTRL + SHIFT + j . In Firefox, install the Firebug plugin.

In your code, add alert() to show the stream and get the values ​​of the variables.

Also, use console.log() , which will only be output to the debug console.

0
source

There are debugging options depending on your browser choice - I tend to use Firefox, so Firebug in my case. There is a question, what are the list options for other browsers - What is console.log and how to use it?

If the project you are working on has already adopted a mechanism for debugging, console.log() tends to be a simple and useful option when looking for a problem.

During debugging, you can use the approach to fail when entering a function, for example:

 var myFunc = function(el) { console.log('Inside myFunc'); // Existing code }; 

This will allow you to see what functions were called and give you a general idea of ​​the execution order.

You can also use console.log() to display the contents of variables - console.log(el);

Be careful to remove / disable console.log() calls as soon as you are done, as this may cause some problems during production.

0
source

To answer your question in a question,

how can a person creating his first javascript function find this?

Well, when something is wrong in JavaScript, for example, you made a syntax error - the script will stop working from there. However, this will not stop the HTML display, so it may look as if everything is correct (especially if your JS does not change the appearance of the page), but all JS functions will be dead.

That's why we use debugging tools (listed in other answers here) to see what is wrong, and in such cases it is very easy to notice which function has errors and causes the entire script to break. This will probably save a few minutes for your seniors.

The best approach would be to test often so that whenever you encounter errors you can fix them right away.

0
source

Source: https://habr.com/ru/post/1469181/


All Articles