Syntax Errors: What They Are and Why They're a Problem

Even the smallest mistakes can break an app

The dreaded syntax error box

Computer languages have really strict rules, and a syntax error means you’ve broken one of them. Syntax exists in ordinary language; it’s the way words are arranged into sentences so they make sense.

While humans are adaptable and can structure a sentence in many ways and it will still make sense, computers need very exact instructions. If we break the rules a bit, a command may still make sense to a human, but a computer won’t be able to interpret it.

Syntax in Human Language

Imagine someone says to you, “I saw a woman over there with a telescope.” There are a lot of ways you could interpret this sentence:

  • "Using a telescope, I observed a woman who was over there."
  • "I observed a woman, who was over there, and she had a telescope."
  • "I observed a woman and a telescope, they were both over there."
  • "I was over there and I observed a woman who had a telescope."
  • "Sometimes, I go over there and saw a woman using a telescope."

Because you’re human, you can apply context to this sentence. You wouldn’t even consider the last interpretation because you know we don’t use telescopes for cutting people. Based on where ‘over there’ is, and possibly any previous discussion about telescopes, you would probably correctly assume the first or second interpretation.

What is a Syntax Error in a Computer Language?

How syntax errors are handled in different languages can vary a lot, a few examples are given here:

  • Excel: If you type a syntactically incorrect formula into a cell in Excel you'll see #VALUE displayed in the cell. It won’t be explicitly labeled as a syntax error, but that’s exactly what it is.
  • HTML: You can break a lot of rules in HTML and a web page will still display just fine in most browsers. The problem with this is the behavior can become unpredictable. A page may look fine on your browser, but not work on someone else’s. It a good idea to check your code with the W3C’s validation service, which provides detailed information about errors in the HTML code.
  • JavaScript: If there’s a syntax error in your JavaScript, it will prevent the thread with the error from running. However, the rest of the code, contained in other threads, will execute, provided they aren’t dependent on the thread with the error. When running the code in a browser, typically nothing will happen. You won’t get an error message, nor will the code run.

What to Do If You Have a Syntax Error

If your code fails because of a syntax error there are a few things you can do to debug your code.

  1. You need to find out exactly where the syntax error is. In many languages, you'll get detailed error messages informing you where the error is in the code. If you know which instruction has the error, you can check the documentation for examples of correct syntax.

  2. If you’re not sure where the problem is, you could try breaking the code up into smaller sections, seeing if each one works to identify which section contains the error. Repeating this process, it’s possible to determine where the problem is and fix it.

  3. If you need to debug a lot of web code, it might be worth looking at using developer tools.