Skip to content

Commit 7d27956

Browse files
Merge pull request #16 from SuryaKarmakar/IBM-JavaScript-Essentials
Debugging in JavaScript
2 parents af6fdc4 + 83a75f2 commit 7d27956

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Courses/IBM-JavaScript-Essentials/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -1332,3 +1332,49 @@ const intervalID = setInterval(() => {
13321332
}
13331333
}, 1000);
13341334
```
1335+
1336+
## Debugging in JavaScript:
1337+
1338+
Debugging in JavaScript is the process of of identifying and fixing errors, bugs and issues in your JavaScript code. These errors can include syntax errors, logical errors, runtime errors, and other issues that prevent your JavaScript program from functioning as intended.
1339+
1340+
1. the simplest form of debugging involves adding console.log statements to your code.
1341+
1342+
2. You can insert the debugger statement directly into your code to halt execution and trigger the browser's debugger.
1343+
1344+
3. Every major web browser includes developer tools that provide debugging capabilities. You can access these tools by pressing F12 or right-clicking on a web page and selecting inspect or inspect element.
1345+
1346+
4. Try Catch block in JavaScript is a structured mechanism used for error handling. It allows you to manage and recover from runtime errors that might occur during the execution of your code.
1347+
1348+
- Try Catch block:
1349+
1350+
If an error occurs within the try-block, the catch block is executed. The catch block specifies how to handle the error. It takes an error object as a parameter which contains information about the error. Try-catch blocks allow you to isolate potential issues by wrapping specific code segments in try blocks you can focus on debugging those sections individually, making it easier to identify the root cause of errors.
1351+
1352+
When an unhandled error occurs in JavaScript, it can lead to application crashes or abrupt behavior. Try-catch blocks prevent this by handling errors, allowing your application to continue running.
1353+
1354+
The error object passed to the catch block contains valuable information such as the error type, message and stack trace.
1355+
1356+
You can use the throw statement within a try block to throw custom expectations based on specific conditions. This enables you to provide more meaningful error information, aiding in debugging.
1357+
1358+
```js
1359+
const array = [1, 2, 3];
1360+
1361+
// This code will throw an error because you cannot access properties of undefined. (reading 'name')
1362+
console.log(array[5].name); // here code execution get stoped.
1363+
1364+
// And this message will never be reached will not be logged.
1365+
console.log("hello world");
1366+
```
1367+
1368+
```js
1369+
try {
1370+
const array = [1, 2, 3];
1371+
1372+
// If an error occurs, it is caught by the catch block and we log an error message.
1373+
console.log(array[5].name); // here the script continues to execute after error handling.
1374+
} catch (error) {
1375+
console.log(error);
1376+
}
1377+
1378+
// the message program continues after error handling is logged.
1379+
console.log("hello world");
1380+
```

0 commit comments

Comments
 (0)