What are the errors in JavaScript

An error is a problem that stops your program from running as expected. In JavaScript, errors can happen at different stages, but the ones you will deal with most are runtime errors.
For example
let user = null;
console.log(user.name);
This throws an error because you are trying to access a property on null.
Common types of errors you will see:
Syntax errors
Reference errors
Type errors
Errors like these can break your application if you do not handle them.
Why error handling matters
If your app crashes every time something goes wrong, users will leave. Error handling helps you control what happens when things fail.
Instead of crashing, your app can:
Show a useful message
Retry an operation
Log the issue for debugging
This is called graceful failure. The app continues to work even when something goes wrong.
Using try and catch
JavaScript provides a built-in structure to handle errors using try and catch.
try {
let data = JSON.parse("invalid json");
} catch (error) {
console.log("Something went wrong:", error.message);
}
How it works:
Code inside
tryruns normallyIf an error occurs, execution jumps to
catchThe error object gives details about what went wrong
This prevents your program from crashing and gives you control over the failure.
The finally block
The finally block runs no matter what happens. Whether the code succeeds or fails, this block always executes.
try {
console.log("Trying...");
} catch (error) {
console.log("Error occurred");
} finally {
console.log("This always runs");
}
You usually use finally it for cleanup tasks, like:
Closing connections
Stopping loaders
Releasing resources
Throwing custom errors
Sometimes you need to create your own errors instead of relying on JavaScript defaults.
function withdraw(amount, balance) {
if (amount > balance) {
throw new Error("Insufficient balance");
}
return balance - amount;
}
Here, you are manually throwing an error using throw.
This is useful when:
You want to enforce business rules
You need clear and meaningful error messages
You want to stop execution intentionally
You can then handle this error using try and catch.
Execution flow of try, catch, finally
Here is the basic flow:
Code enters the try block
If no error occurs, catch is skipped
If an error occurs, catch runs
Finally runs in both cases
Think of it like a safety net. The try block is your main logic, catch handles failures, and finally ensures cleanup.
A simple real-world example
function fetchUserData() {
try {
let data = JSON.parse('{"name": "John"}');
console.log(data.name);
} catch (error) {
console.log("Failed to fetch user data");
} finally {
console.log("Request completed");
}
}
fetchUserData();
Even if parsing fails, your program continues to run and gives useful output.
Debugging benefits
Error handling also makes debugging easier.
Instead of random crashes, you get:
Clear error messages
Controlled logs
Better understanding of where things failed
This saves time when your application grows larger.
Conclusion
Errors are part of writing JavaScript, and ignoring them leads to unstable applications. By using try, catch, and finally, you can control how your code behaves when something goes wrong.
Once you start throwing your own errors and handling them properly, your code becomes more predictable and easier to maintain.
Handling errors well is not an advanced skill. It is a basic habit that every developer should build early.




