Kaunia
Errors and bugs are inevitable in programming. What matters is how you handle and debug them. JavaScript provides built-in mechanisms for error handling (like try/catch/finally and throw) and debugging tools (such as developer console, breakpoints, and the debugger keyword). In this post, you will learn:
When an error occurs, JavaScript creates an Error object that has two main properties:
You can throw custom errors using the throw statement, passing any value you want (string, number, object).
You wrap code that might throw an error with try, and if an error happens, it is caught in catch. Optionally, finally runs code no matter what, whether error happened or not.
Example:
You can throw errors deliberately to signal invalid states or unexpected inputs:
You can throw built-in Error objects too:
Throwing custom or built-in errors gives you control over error reporting and handling.
Some error names you’ll often see are:
Debugging is the process of finding and fixing errors or unexpected behavior in code. In JavaScript, errors sometimes fail silently (no visible effect), which makes debugging critical.
A simple but powerful debugging technique is using console.log() to output variable values or messages to the developer console.
You can also use console.error() for logging errors explicitly:
This helps highlight error messages in the console.
Modern browsers have built-in debugging tools. You can open them (often via F12) and:
You can insert a debugger; statement in your code to force a breakpoint at that location (if a debugger is open). If no debugger is active, it has no effect.
When the code runs and reaches debugger;, execution will pause if dev tools are open.
Type the number that you see in image.
JavaScript Master – Learn, Code & Build Dynamic Websites
javascript-mastery-0
Buy Electronics & Gadgets & Devices - Electro Seller
electro-seller-0
The London Eye
the-london-eye
Website Consultation Services | Book a Web Design Expert Today
website-consultant-0
Book Rooms Online in the UK | Hotels, Apartments & Stays
book-room-uk