The JavaScript exception "missing ; before statement" occurs when there is a semicolon (;
)
missing somewhere and can't be added
by automatic semicolon insertion (ASI).
You need to provide a semicolon, so that JavaScript can parse the source code correctly.
Message
SyntaxError: Expected ';' (Edge)
SyntaxError: missing ; before statement (Firefox)
Error type
What went wrong?
There is a semicolon (;
) missing somewhere. JavaScript statements must
be terminated with semicolons. Some of them are affected
by automatic semicolon insertion (ASI),
but in this case you need to provide a semicolon,
so that JavaScript can parse the source code correctly.
However, oftentimes, this error is only a consequence of another error, like not
escaping strings properly, or using var
wrongly. You might also have too
many parenthesis somewhere. Carefully check the syntax when this error is thrown.
Examples
Unescaped strings
This error can occur easily when not escaping strings properly and the JavaScript engine is expecting the end of your string already. For example:
const foo = 'Tom's bar';
// SyntaxError: missing ; before statement
You can use double quotes, or escape the apostrophe:
const foo = "Tom's bar";
// OR
const foo = 'Tom\'s bar';
Declaring properties with keyword
You cannot declare properties of an object or array with a
let
, const
, or var
declaration.
const obj = {};
const obj.foo = "hi"; // SyntaxError missing ; before statement
const array = [];
const array[0] = "there"; // SyntaxError missing ; before statement
Instead, omit the keyword:
const obj = {};
obj.foo = "hi";
const array = [];
array[0] = "there";
Bad keywords
If you come from another programming language, it is also common to use keywords that don't mean the same or have no meaning at all in JavaScript:
def print(info) {
console.log(info);
} // SyntaxError missing ; before statement
Instead, use function
instead of def
:
function print(info) {
console.log(info);
}