Brainlag
  1. Library
  2. Debugging
  3. Web

SyntaxErrorWhat is a SyntaxError?

A SyntaxError means JavaScript could not even read your code, so nothing ran. Usually a missing bracket, brace, quote or comma.

Web5 min

What JavaScript is telling you

Output
SyntaxError: missing ) after argument list
SyntaxError: Unexpected end of input
SyntaxError: Unexpected identifier 'score'

Every other error happens while the code runs. A SyntaxError happens before: JavaScript reads the whole program first, hits something that is not valid JavaScript, and refuses to start. That is why not even the first console.log prints.

The usual reasons: a (, { or [ that never closes, a string missing its closing quote, a missing comma between items in an array or object, or a stray character.

Unexpected end of input means the file ended while JavaScript was still waiting for a closing } or ).

How to find it

The message says what JavaScript expected, but the line it points at (when it shows one) is where it gave up, which can be after the real mistake. Start there and read backwards. Count brackets in pairs: every opener needs a closer. Good indentation makes a missing } easy to spot, because the code below it lines up wrong.

Run it: a bracket that never closes

Press Run and read the error from the bottom up.

main.js to run
const song = "Neon Rain";
const plays = 1520;

console.log("Now playing:", song;
console.log("Plays:", plays);

A shop that never opens

Nothing prints at all. Find the mistakes and fix them so it prints:

Output
Hoodie: 35
Cap: 12
Socks: 6
exercise_1.js to run
const shop = [
  { item: "Hoodie", price: 35 },
  { item: "Cap", price: 12 }
  { item: "Socks", price: 6 },
];
for (const product of shop) {
  console.log(product.item + ": " + product.price);

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.