What a syntax error is
A syntax error is a mistake in the way a program is written, like a spelling or grammar mistake in English. The sentence "Would you some tea?" is missing a verb, and a reader notices at once. The compiler is just as strict. If the code breaks the rules of the language, it can't work out what you meant, and it refuses to build the program.
TypeScript code is checked by the compiler, tsc, which reports problems as errors with a code that starts with TS, a position, and a short message. For ordinary type errors the compiler still writes the JavaScript file. A syntax error is different in practice: the compiler guesses what you meant and writes JavaScript anyway, but that JavaScript may not be valid. With an unterminated string, Node refuses to run the file and throws SyntaxError: Invalid or unexpected token. With a missing closing parenthesis, the compiler's guess can happen to run, so a program with the error may still print something. Either way, the reported error is the thing to fix.
Typical syntax errors
A missing closing quote
Every string needs a quote at both ends, of the same kind:
console.log("Welcome!);
The string never ends, so the compiler reports:
error TS1002: Unterminated string literal.
Mixing the kinds has the same effect. In let greeting = 'Hey there!"; the string opens with a single quote, so the double quote doesn't close it.
A missing parenthesis
A call needs its opening and closing parentheses:
console.log("Let's code!";
error TS1005: ')' expected.
If the opening one is missing instead, as in console.log"My name is Andy");, the compiler reports ';' expected. The message names the first place where the code stopped making sense, which is not always where the mistake is. If the line shown looks fine, check the line above it.
A related error: names that don't exist
Another common mistake is a misspelled name. This is not a syntax error, because the code is written correctly, but the name means nothing:
consle.log("My name is Andy");
error TS2552: Cannot find name 'consle'. Did you mean 'console'?
The same happens with a variable. After let animal = "parrot";, the call console.log(aniimal) reports that it cannot find the name aniimal. TypeScript catches this while compiling. In plain JavaScript, the same mistake only shows up when the line runs, as a ReferenceError.
How to read an error message
- Find the position. The message includes a line and a column, written like
(1,21). Start at that line. - Read the message.
')' expectedmeans a closing parenthesis is missing.Unterminated string literalmeans a quote is missing. - Look at the line before it. An unclosed bracket or quote can make the error appear later than the real mistake.
- Fix one error and compile again. One mistake can cause several messages, and fixing the first often clears the others.
For example, the two broken lines in the exercise produce three messages together: a missing ) on line 1, an unterminated string on line 2, and a second missing ) reported at the start of line 3, because the call on line 2 never closes either.
The exercise
The starter code has two lines that should print Hello! and I love coding!, but each has a small syntax error:
console.log("Hello!";
console.log("I love coding!);
Steps to fix it:
- On line 1 the string is closed, but the call is missing its closing parenthesis. Add
)before the semicolon. - On line 2 the string is missing its closing double quote. Add
"after the exclamation mark. - Line 2 then also needs its closing parenthesis and semicolon, so it ends with
");.
The corrected program compiles without errors and prints the two messages, one per line. The lesson's tests check the output, so keep the text exactly as given.
Key takeaways
- A syntax error means the code breaks the language's rules, so the compiler reports it and its output may not be valid.
- Every opening quote and parenthesis needs a matching closing one.
- The reported position is where the compiler got lost, so check the line above as well.
- A misspelled name is reported as
Cannot find name, reported by the compiler.