JavaScript Debugging - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
JavaScript Debugging

JavaScript Debugging

JavaScript Debugging

Sometimes a code may contain certain mistakes. Being a scripting language, JavaScript didn't show any error message in a browser. But these mistakes can affect the output.
The best practice to find out the error is to debug the code. The code can be debugged easily by using web browsers like Google Chrome, Mozilla Firebox.

JavaScript Debugging Example

Here, we will find out errors using built-in web browser debugger. To perform debugging, we can use any of the following approaches:
  • Using console.log() method
  • Using debugger keyword

Using console.log() method

The console.log() method displays the result in the console of the browser. If there is any mistake in the code, it generates the error message.
Let's see the simple example to print the result on console.
  1. <script>  
  2. x = 10;  
  3. y = 15;  
  4. z = x + y;  
  5. console.log(z);  
  6. console.log(a);//a is not intialized  
  7. </script>  
Output:
To open the console on browser, press F12 key.
Javascript Debugging

Using debugger keyword

In debugging, generally we set breakpoints to examine each line of code step by step. There is no requirement to perform this task manually in JavaScript.
JavaScript provides debugger keyword to set the breakpoint through the code itself. The debugger stops the execution of the program at the position it is applied. Now, we can start the flow of execution manually. If an exception occurs, the execution will stop again on that particular line.
  1. <script>  
  2. x = 10;  
  3. y = 15;  
  4. z = x + y;  
  5. debugger;  
  6. document.write(z);  
  7. document.write(a);  
  8. </script>  
Output:
Javascript Debugging

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.