How to Find Errors in the JavaScript

Every programming language comes up with errors, and JavaScript is not an Exception. Tools like VS Code made it easy to find errors in JavaScript.

JavaScript language does compile, BUT developers of browsers hide this compilation process. That’s why we say JavaScript code doesn’t compile, but that’s not true. It does.

Because of the compilation process is hidden from end developers, it is challenging to find compile errors in the JavaScript when you write code. But code editor like VS code points out compile errors to you.

Most of the compile errors in JavaScript don’t harm the entire application as it does in the languages like C# and Java. If you have errors in the JavaScript code, you still see the website is running in the browser, and functionalities of the application are working fine.

So now how to find errors in the JavaScript if your code is not working.

Process 1 : Errors and Warnings in Code Editor

If you are using any code editor, then you can efficiently resolve errors. Let’s look at the example of Visual Studio Code Editor below.

Javascript-Errors-in-VS-Code

In the above snippet, you can see that I deliberately removed parentheses before the semicolon. VS Code is smart enough to tell you that you are missing closing parentheses. Also, on the left, you can see index.js file turned Red showing one error in the code.

Let’s look at another example. Here VS Code also failed to find an error in code.

Error-in-function-in-VS-Code

In the above snippet, you can see that I have misspelled alert function name. But VS Code saying nothing! In this case, you can run your code without realizing your mistake, and you will find that browser is not showing alert pop up.

What would you do in this case?

Process 2 : Errors Inside Console Window of Browser

Run your code. When the browser opens, You will see that your functionality is not working. In the above case, the alert box will not appear because of the wrong spelling of function name. To find an error, Go to the developer tools of the browser. You can find it in a menu of browser or press F12 to open it.

In that, you will find the Console tab. Click on that you will find the error. Fix the error in the code by reading instruction given in the console.

In the case of an alert function, the wrong spelling caused the error logged in the console window of the chrome browser.

Error-in-Console-Window

You can see that there is an error that says ‘aler is not defined.’ We know that we have misspelled alert function name. There is no function defined with name aler. Fix it. Pop up will appear.

There are JavaScript libraries and frameworks like react and angular, which has there inbuilt ways to point out errors. But the above methods are the most common ways to find any error in the JavaScript.

That’s it. This is how we find errors in the JavaScript.

Categories js

Leave a Comment