Copyright

Errors in JavaScript: Types, Methods & Prevention

Instructor: Prashant Mishra

Prashant is currently pursuing his bachelors in Computer Science and Engineering.

In this lesson, errors in JavaScript will be defined with their types explained. Removal of these errors with the onerror() method and the concept of exceptional handling will also be discussed.

Errors in JavaScript

Errors are statements which don't let the program run properly. There are three main types of errors that can occur while compiling a JavaScript program. These errors include syntax errors, runtime errors, and logical errors.

Syntax Errors

Syntax errors are the most common type of error that occurs in any programming language. As the name suggests, something incorrect in the syntax of the program body raises this error. Syntax errors are also known as parsing errors. In JavaScript, they occur at the interpretation time.

Consider this code segment:

  <script type=''text/javascript''>
    window.show(;
  </script>

This program will raise an error as the closing bracket of the show function is missing.

Runtime Errors

These type of error occurs during the runtime of the program, after it is interpreted by the compiler.

Consider this code segment:

  <script type=''text/javascript''>
    window.show();
  </script>

Notice that there is no show function defined. This program will raise an error at runtime as the function which is not present is called, although the syntax is correct.

Logical Errors

These type of errors are the most difficult to find. Consider a statement: ''John is playing guitar.'' This statement is logically correct and its syntax is also correct. Now consider another statement: ''Guitar is playing John.'' This statement is correct with respect to its syntax but is logically incorrect. These types of errors cause a serious problem as they change the whole path of how your program will work.

Methods to Prevent Errors

Onerror() Method

In this method, an error event is fired when an error occurs. This method also works for those errors caused by external sources. Whenever an error is caught, this method tells the developer the line number at which the error has occurred, the file to which the error has occurred and displays a customized error message.

Consider the following code segment:

  <html>
  <head>
  <script type=''text/javascript''>
    window.onerror = function () {
    alert(''An error has occurred. Please resolve'');
    }
  </script>
  </head>
  <body>
  <p>Click to see the result:</p>
  <form>
  <input type = 'button' value = 'Click' onclick = 'myFunc();' />
  </form>
  </body>
  </html>

When the button is clicked, the error message ''An error has occurred. Please resolve'' is displayed.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support