Home > Articles

Bugs -- What They Are and How to Get Rid of Them!

  • Print
  • + Share This
Bugs are problems with code, and the process of tracking down these bugs and destroying them is called debugging. In this excerpt from JavaScript 1.5 by Example, authors Adrian and Kathie Kingsley-Hughes take a look at common JavaScript errors that JavaScripters (new and old) make when creating scripts.
This article is derived from JavaScript 1.5 by Example .
This chapter is from the book

This chapter is from the book

What are Bugs?

Bugs are problems with code, and the process of tracking down these bugs and destroying them is called debugging. But why such a strange analogy?

Well, in reality, no one really knows, but the term's origin has been wrongly attributed to the mid-40s pioneer programmer Grace Hopper. In 1944, Grace Hopper, a young Naval Reserve officer (she later became Admiral Hopper), was working on the Mark I computer at Harvard as one of the first people to write computer programs for it. While she was working with the later Mark II, a technician is said to have pulled a moth from between two electrical relays in the computer, the "bug" that was keeping the program from running. This moth was apparently kept on display by the Navy for many years and is now the property of the Smithsonian.

Debugging is the name given to the process of finding and fixing (or sometimes bypassing) these computer bugs.


Don't be put off by bugs in your JavaScript. It's easy to get disheartened in the beginning, but rest assured, you'll soon develop an eye for them and be able to spot them early on before they become a problem.

Types of Bugs

Just like the real world, which has many species of creepy-crawlies, the computer world has many types of computer bugs. There are, in fact, a few different "species" of computer bugs. Some of the most common are:

  • Syntax errors

  • Runtime errors

  • Logic errors

  • Incorrect use of operator precedence

Let's take a look at what each of these means.

Introducing Syntax Errors

Syntax errors are the most common type of error programmers come across. Syntax is the rules of grammar and spelling for a computer language, and, because computers are more picky about language than we humans, you need to be very careful when communicating with them. You have to learn to obey their rules. This means, for instance, that when you use a string, you must enclose it in quotes. And there is no point thinking you can leave out one of the quotes—the computer will catch you!

JavaScript code is examined for syntax errors as it is being loaded into the browser, so any errors show up quickly.

The following is an example of such an error:

txtString ="Hello there

Another example of a syntax error is poor spelling:

fnction callMe()
alert("This script has a syntax error in the spelling of function ");

Runtime Errors

Runtime errors occur as the JavaScript script tries to perform something the system cannot do. They are called runtime errors because they can occur at any time during the lifetime of the script. A common example of a runtime error is trying to call a function that doesn't exist (such as when you misspell the function name):

<title>A Simple Page</title>
<script language="JavaScript">
<!--Cloaking device on!
function myFuncion()
alert("Hello there");
//Cloaking device off -->
<body onLoad="myFunction()">

Logic Errors

Logic errors aren't really JavaScript errors but errors in the way the script works. You might have a script that calculates sales tax, but instead of adding it to the total, the script subtracts it. This is a logic error.

Operator Precedence

Operator precedence is similar to a logic error but relates to how operators are dealt with in mathematics.

The following is an example:

ans = num1 - num2 * num3;

If you assign arbitrary numbers to num1 , num2 , and num3—say 3, 1, and 6—what will the answer be? Will it be 12 (3 minus 1 equals 2, multiplied by 6) or -3 (6 multiplied by -1 equals -6, plus 3)? In fact, the answer will be –3 because of the order in which operators are processed. Multiplication is carried out before subtraction.

The order of operator importance is as follows:



--and ++

Unary operators

*, /, and %

Multiplication, division, and modulo division

+and -

Addition and subtraction

<, <=, >, and >=

Less than, less than or equal to, greater than, and greater than or equal to

==and !=

Equality and inequality


Logical AND


Logical OR




Those of you familiar with algebraic precedence will find this list familiar because they are the same.

Parentheses are used to alter the order of evaluation determined by operator precedence. This means an expression within parentheses is evaluated before its value is used in the remainder of the expression. So, if you wanted the previous example to equal 12, you would write it as follows:

ans =(num1 -num2)*num3;

The following example proves this point:

<script language="JavaScript ">
<!--Cloaking device on!
alert(3 -1 *6); // comes to –3
alert((3 -1)*6); // comes to 12
//Cloaking device off -->
  • + Share This
  • 🔖 Save To Your Account