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

Even the smallest mistakes can break an app

Computer languages impose strict rules. A syntax error means one of those rules is broken. Syntax exists in ordinary language. It's the way words are arranged into sentences to make sense.

Humans are adaptable. They can structure a sentence in many ways, and it will still make sense. In contrast, computers require precise instructions. If you break the rules a bit, a command may make sense to a human, but a computer won't be able to interpret it.

Syntax error in programming
Valentin Wolf / Getty Images

Syntax in Human Language

Imagine someone says to you, "I saw a woman over there with a telescope." There are several ways to 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 consider the last interpretation because you know we don't use telescopes for sawing people as if they were slices of bread. 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. For example:

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

What to Do If You Have a Syntax Error

Debug your code if you encounter a syntax error.

  1. Identify the location of the error. You'll encounter detailed error messages in many languages, 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 the correct syntax.

  2. If you're not sure where the problem is, break the code into smaller sections, seeing if each one works to identify which section contains the error. When you repeat 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, consider using developer tools.