Everything about JavaScript errors…30daysofjavascript => Day 9

Olajide Blessing Niniola
4 min readMar 26, 2021

Ouch, now what have I done wrong? In programming, there are bound to be errors, we run into bugs and get errors in our console. Now, that is on the programmer’s side, what about the client-side? Take, for instance, a user is trying to open an account and there has to be a special username because there can only be a username for one user as two users can not have the same and exact username, of course, the user gets an error!!!

There are three major kinds of errors when programming in JavaScript

  • Syntax error
  • Logical error
  • Runtime error

Syntax Error: By now, we already know that syntax is like formulas to how a block of code is executed. A syntax error is what you get when you write the wrong syntax, this could perhaps be due to wrong spellings or missing a step. In the code snippet below, the function is misspelt. Syntax errors can be easy to solve as soon as you see them. I found an interesting list of syntax errors here.

Syntax Error

Logical Errors: This error could be a little difficult to solve because they do not log any error to the console, they do not just give the desired result even though the syntax is correct. The example below shows a logical error:

Of course, we had expected our answer to be 41 but we have here 2021. Now, what could be the problem? The syntax is correct but remember, any value in a colon (whether it is a number or letter) is seen as a string. If we remove the colon we get the desired answer.

Handling Errors

  • Try and catch: The try and catch statement comes in pairs (i.e. they are always used together). The try statement allows you to define a block of code to be tested for errors during its execution. The catch statement allows you to define a block of code to be executed if an error occurs in the try block. It uses the syntax below:

try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}

Try and Catch
  • The Throw Statement: Normally JavaScript generates an error message when there is an error. This is technically called throw and error or throw an expectation. The throw statement allows you to create custom errors, the exception can be a string, a number, a Boolean or an object. If you use throw together with try and catch, you can control program flow and generate custom error messages. This statement is mainly used on the client-side.
a throw statement

The finally Statement: The finally statement lets you execute code, after try and catch, regardless of the result. The finally statement uses the syntax below

try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
finally {
Block of code to be executed regardless of the try / catch result
}

The Error Object

JavaScript has a built-in error object that provides error information when an error occurs. The error object provides two useful properties: name(this sets or returns an error name) and message (this sets or returns an error message usually in form of a string).

Error Values

Conclusion

Having errors does not entirely mean you have written bad code, every programmer run into errors.

--

--