SlideShare a Scribd company logo
Team Emertxe
www.webstackacademy.com
Debugging Techniques
JavaScript
www.webstackacademy.com ‹#›www.webstackacademy.com
JavaScript Errors
(Capturing errors using JavaScript constructs)
Errors
In JavaScript three types of error are:
 Syntax Error: Occurs at compile / interpreting time
 Run Time Errors: Happens during execution. Exceptions and handling
happens here
 Logical Error: Occurs when we make mistake in logic
The try-catch
Syntax:
try
{
//statements
}
catch (error)
{
//statements
}
The Statement try is used to enclose and test parts of
the program where some problem is expected. If an
exception is encountered the control is shifted to catch
block.
To the catch block the problem is returned in form of
error object, which has two properties:
Name : Name of the error (category)
Description: Details about the error
The try-catch example
<script>
function errorFunc()
{
try {
// Write some junk
somejunk();
}
catch(e){
alert("Error Name : " + e.name);
alert("Error Message : " + e.message);
}
}
</script>
<body>
<button onclick=“errorFunc()”>Test Error Function</button>
</body>
The Error Object
Methods Description
RangeError A number "out of range" has occurred
ReferenceError An illegal reference has occurred
SyntaxError A syntax error has occurred
TypeError A type error has occurred
URIError An error in encodeURI() has occurred
The finally statement
The finally clause is used to execute
statements after the end of try block,
whether or not an exception occurred
within the try block.
Syntax:
try
{
// statements
}
catch (error)
{
// statements
}
finally
{
// statements
}
The throw statement
The throw statement allows to create
user defined conditions for
exceptions.
Syntax:
try
{
// statements
throw “statements”;
}
catch (error)
{
// statements
}
JavaScript Throw Example
<script>
function errFunc()
{
var x = Number(prompt("enter x value"));
var y = Number(prompt("enter y value"));
try{
if ( y == 0 ){
throw( "Divide by zero error." );
}
else
{
var z = x / y;
document.write("z ="+z+"<br>");
}
}
catch ( e ) {
alert("Error: " + e );
}
}
</script>
Exercise
• Write a JavaScript program to enter the age of any person and if age is less
than 18 then throw an exception “not eligible for voting”
• Write a JavaScript program to enter the number between 5 to 20. If the
number is not within range then throw an user defined exception
www.webstackacademy.com ‹#›www.webstackacademy.com ‹#›
Debugging
(Debugging the running program using debugger provided by browser)
Debugging
• All modern browsers have a built-in debugger. In our case Chrome browser is provided as a
case study
• These debuggers provide facility to walk through the program during run-time
• It will give you the live snapshot of the program, even alter the flow of the program by
forcing variable values
• By carefully investigating all the facilities provided by the debugger developers can empower
themselves
• However please note before getting into this run-time debugging, ensure previous steps
(Requirements understanding, Algorithm Design, Pseudo-code, Dry-run) are followed well.
• Best way to solve a problem is to avoid them 
The “debugger” keyword
• The debugger keyword stops the execution of JavaScript and calls the debugging
function
• To view the debugger window press F12
<script>
var x = 4 * 5;
debugger; //stop executing before its executes next line
document.getElementById("ex").innerHTML = x;
</script>
The Debugger window
The Debugger window
Exercise
• Check the given program and do the following:
• Various facilities provided
• Run-time walk through of the code
• Understand step-in and step-through options and differences between them
• Understand various segments of a running program (Code, Data, Stack, Heap)
• What is the call-stack and how it plays a role in function handling?
Breakpoints
 Break Points can be used to stop the execution of the code. They can
be set directly in the debugger without using ‘debugger’ keyword
 Multiple break-points can be set at required placed to monitor the
code flow. It will help you to investigate the source code at various
locations
 You can resume the execution of code by pressing the ‘play’ button, it
will further run or pause in the next break-point
 Depending on the issue, breakpoints will help you to narrow down to
the code area where the potential problem is there. Upon careful
investigation fixes can be done
The Debugger window
Changing variable values in runtime
 There can be some situations during development / testing as follows:
 Developer not able to test various paths of the code (ex: if…else)
 Developer not able to exactly recreate the problem but knows the path
 Developer want to force certain conditions and see how the source code handles
• In such situations it would be helpful if the developer is able to force certain
variable value during runtime
• This is a crude approach, but helps during the development time. Achieving
100% test coverage using external methods may not be possible all the
times
• In Chrome you need to use the ‘console’ tab to set values
The Debugger window
Exercise
• Check the given program and do the following:
• Remove debugger statement
• Setting multiple break-points and use play button to move between
• Investigate run-time snapshot between multiple break-points
• Force the code to take a different path in the if…else condition
• Add / Remove multiple break-points
www.webstackacademy.com ‹#›www.webstackacademy.com
Avoiding Mistakes
(JavaScript – Strict Mode)
Strict Mode
• Strict mode is declared by adding "use strict"; to the beginning of a script
or a function.
• Declared at the beginning of a script, it has global scope.
"use strict";
// This will cause an error (x is not declared).
x = 3.14;
Strict mode
• Strict mode makes it easier to write "secure" JavaScript.
• Objects are variables too so without declaring object we cannot
initialize.
• In strict mode certain operations are not permitted (ex: Deleting an
object) are not allowed. This also helps to keep code safe by avoiding
some mistakes.
"use strict";
x = {p1:10, p2:20}; // error
WebStack Academy
#83, Farah Towers,
1st Floor, MG Road,
Bangalore – 560001
M: +91-809555 7332
E: training@webstackacademy.com
WSA in Social Media:

More Related Content

What's hot (20)

PPTX
Intro to React
Justin Reock
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PDF
Asynchronous JavaScript Programming
Haim Michael
 
PDF
Introduction into ES6 JavaScript.
boyney123
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
Angularjs PPT
Amit Baghel
 
PPTX
ASP.NET Web API
habib_786
 
PDF
ReactJS presentation
Thanh Tuong
 
PPT
JavaScript Tutorial
Bui Kiet
 
PPT
Angular 8
Sunil OS
 
PDF
React js
Rajesh Kolla
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PPT
JQuery introduction
NexThoughts Technologies
 
PPTX
MERN PPT
NeerajGupta96647
 
PPTX
React workshop
Imran Sayed
 
PPTX
Angular 9
Raja Vishnu
 
PPTX
React js
Oswald Campesato
 
PPT
C#.NET
gurchet
 
Intro to React
Justin Reock
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Asynchronous JavaScript Programming
Haim Michael
 
Introduction into ES6 JavaScript.
boyney123
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Angularjs PPT
Amit Baghel
 
ASP.NET Web API
habib_786
 
ReactJS presentation
Thanh Tuong
 
JavaScript Tutorial
Bui Kiet
 
Angular 8
Sunil OS
 
React js
Rajesh Kolla
 
JavaScript Promises
L&T Technology Services Limited
 
JQuery introduction
NexThoughts Technologies
 
React workshop
Imran Sayed
 
Angular 9
Raja Vishnu
 
C#.NET
gurchet
 

Similar to JavaScript - Chapter 15 - Debugging Techniques (20)

PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
Master Cucumber cheat sheet for testing .pdf
ArunVastrad4
 
PDF
8.-Javascript-report powerpoint presentation
JohnLagman3
 
PPT
F6dc1 session6 c++
Mukund Trivedi
 
PDF
Guide to Playwright Debugging – Tips and Techniques.pdf
Steve Wortham
 
PDF
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
wabii3179com
 
PDF
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Yasuharu Nakano
 
PPTX
Javascript
Nagarajan
 
PDF
[xp2013] Narrow Down What to Test
Zsolt Fabok
 
PPTX
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
PDF
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
 
PPTX
Testing Ext JS and Sencha Touch
Mats Bryntse
 
DOC
Software Bugs A Software Architect Point Of View
Shahzad
 
PPT
Pragmatic Parallels: Java and JavaScript
davejohnson
 
PDF
Different Techniques Of Debugging Selenium Based Test Scripts.pdf
pCloudy
 
PPT
My programming final proj. (1)
aeden_brines
 
PPT
9781305078444 ppt ch04
Terry Yoast
 
PPTX
Yeahhhh the final requirement!!!
olracoatalub
 
PPTX
Switch case looping
Cherimay Batallones
 
PPTX
My final requirement
katrinaguevarra29
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Master Cucumber cheat sheet for testing .pdf
ArunVastrad4
 
8.-Javascript-report powerpoint presentation
JohnLagman3
 
F6dc1 session6 c++
Mukund Trivedi
 
Guide to Playwright Debugging – Tips and Techniques.pdf
Steve Wortham
 
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
wabii3179com
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Yasuharu Nakano
 
Javascript
Nagarajan
 
[xp2013] Narrow Down What to Test
Zsolt Fabok
 
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
 
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Software Bugs A Software Architect Point Of View
Shahzad
 
Pragmatic Parallels: Java and JavaScript
davejohnson
 
Different Techniques Of Debugging Selenium Based Test Scripts.pdf
pCloudy
 
My programming final proj. (1)
aeden_brines
 
9781305078444 ppt ch04
Terry Yoast
 
Yeahhhh the final requirement!!!
olracoatalub
 
Switch case looping
Cherimay Batallones
 
My final requirement
katrinaguevarra29
 
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
 
PDF
Building Your Online Portfolio
WebStackAcademy
 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PDF
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
PDF
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
PDF
JavaScript - Chapter 1 - Problem Solving
WebStackAcademy
 
PDF
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
JavaScript - Chapter 1 - Problem Solving
WebStackAcademy
 
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
Ad

Recently uploaded (20)

PDF
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PPTX
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 

JavaScript - Chapter 15 - Debugging Techniques

  • 3. Errors In JavaScript three types of error are:  Syntax Error: Occurs at compile / interpreting time  Run Time Errors: Happens during execution. Exceptions and handling happens here  Logical Error: Occurs when we make mistake in logic
  • 4. The try-catch Syntax: try { //statements } catch (error) { //statements } The Statement try is used to enclose and test parts of the program where some problem is expected. If an exception is encountered the control is shifted to catch block. To the catch block the problem is returned in form of error object, which has two properties: Name : Name of the error (category) Description: Details about the error
  • 5. The try-catch example <script> function errorFunc() { try { // Write some junk somejunk(); } catch(e){ alert("Error Name : " + e.name); alert("Error Message : " + e.message); } } </script> <body> <button onclick=“errorFunc()”>Test Error Function</button> </body>
  • 6. The Error Object Methods Description RangeError A number "out of range" has occurred ReferenceError An illegal reference has occurred SyntaxError A syntax error has occurred TypeError A type error has occurred URIError An error in encodeURI() has occurred
  • 7. The finally statement The finally clause is used to execute statements after the end of try block, whether or not an exception occurred within the try block. Syntax: try { // statements } catch (error) { // statements } finally { // statements }
  • 8. The throw statement The throw statement allows to create user defined conditions for exceptions. Syntax: try { // statements throw “statements”; } catch (error) { // statements }
  • 9. JavaScript Throw Example <script> function errFunc() { var x = Number(prompt("enter x value")); var y = Number(prompt("enter y value")); try{ if ( y == 0 ){ throw( "Divide by zero error." ); } else { var z = x / y; document.write("z ="+z+"<br>"); } } catch ( e ) { alert("Error: " + e ); } } </script>
  • 10. Exercise • Write a JavaScript program to enter the age of any person and if age is less than 18 then throw an exception “not eligible for voting” • Write a JavaScript program to enter the number between 5 to 20. If the number is not within range then throw an user defined exception
  • 11. www.webstackacademy.com ‹#›www.webstackacademy.com ‹#› Debugging (Debugging the running program using debugger provided by browser)
  • 12. Debugging • All modern browsers have a built-in debugger. In our case Chrome browser is provided as a case study • These debuggers provide facility to walk through the program during run-time • It will give you the live snapshot of the program, even alter the flow of the program by forcing variable values • By carefully investigating all the facilities provided by the debugger developers can empower themselves • However please note before getting into this run-time debugging, ensure previous steps (Requirements understanding, Algorithm Design, Pseudo-code, Dry-run) are followed well. • Best way to solve a problem is to avoid them 
  • 13. The “debugger” keyword • The debugger keyword stops the execution of JavaScript and calls the debugging function • To view the debugger window press F12 <script> var x = 4 * 5; debugger; //stop executing before its executes next line document.getElementById("ex").innerHTML = x; </script>
  • 16. Exercise • Check the given program and do the following: • Various facilities provided • Run-time walk through of the code • Understand step-in and step-through options and differences between them • Understand various segments of a running program (Code, Data, Stack, Heap) • What is the call-stack and how it plays a role in function handling?
  • 17. Breakpoints  Break Points can be used to stop the execution of the code. They can be set directly in the debugger without using ‘debugger’ keyword  Multiple break-points can be set at required placed to monitor the code flow. It will help you to investigate the source code at various locations  You can resume the execution of code by pressing the ‘play’ button, it will further run or pause in the next break-point  Depending on the issue, breakpoints will help you to narrow down to the code area where the potential problem is there. Upon careful investigation fixes can be done
  • 19. Changing variable values in runtime  There can be some situations during development / testing as follows:  Developer not able to test various paths of the code (ex: if…else)  Developer not able to exactly recreate the problem but knows the path  Developer want to force certain conditions and see how the source code handles • In such situations it would be helpful if the developer is able to force certain variable value during runtime • This is a crude approach, but helps during the development time. Achieving 100% test coverage using external methods may not be possible all the times • In Chrome you need to use the ‘console’ tab to set values
  • 21. Exercise • Check the given program and do the following: • Remove debugger statement • Setting multiple break-points and use play button to move between • Investigate run-time snapshot between multiple break-points • Force the code to take a different path in the if…else condition • Add / Remove multiple break-points
  • 23. Strict Mode • Strict mode is declared by adding "use strict"; to the beginning of a script or a function. • Declared at the beginning of a script, it has global scope. "use strict"; // This will cause an error (x is not declared). x = 3.14;
  • 24. Strict mode • Strict mode makes it easier to write "secure" JavaScript. • Objects are variables too so without declaring object we cannot initialize. • In strict mode certain operations are not permitted (ex: Deleting an object) are not allowed. This also helps to keep code safe by avoiding some mistakes. "use strict"; x = {p1:10, p2:20}; // error
  • 25. WebStack Academy #83, Farah Towers, 1st Floor, MG Road, Bangalore – 560001 M: +91-809555 7332 E: [email protected] WSA in Social Media: