Tuesday 11 March 2014

Javascript : How to debug?

Debugger -
It allows you to find the mistakes in your written programme
There can be chance that you would make a mistake while writing your programme.
and that mistake is referred to as a bug.

The process of finding and fixing bugs is called debugging and is a normal
part of the development process.

Now we will discuss tools and techniques that can help you to debug code.

I am using chrome as Example :

To see where error is throwing
-enter F12 (which will open inspect element) -> go to console
-you'll see character written with red color (showing error)
-see the rightmost side of error characters -> some js file will be there
->click on js file it will show you the error location
                                         
To debug the code :
In previous example we understood how to find the place of error in js file
Now we will fix the error
-> put the "debugger;" (without quotes) just before error is throwing or just
   inside the function in which error exist

Example :

function checkthis(){
debugger;
// your code here
}

Note :
while debugging you can go to console and check the value of variables

Example:

funtion checkthis(data){
debugger;
var b = data.item; // you can check this b value in console after debugger stepover this line
}

Javascript validator tools :
The most convenient validator for JavaScript is Douglas Crockford's
JavaScript Lint.
http://www.jslint.com/