Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

SyntaxError

A syntax error — in code, in JSON or in a regular expression.

Available since: ES3

Syntax

new SyntaxError(message)

Parameters

ParameterTypeDescription
messagestringOptional. A description of the error.

Return value

SyntaxErrorAn error object.

Examples

Invalid JSON:

try {
  JSON.parse("{'name': 'Anna'}");
} catch (error) {
  console.log(error.name);
}

Output:

SyntaxError

An invalid regular expression:

try {
  new RegExp('(abc');
} catch (error) {
  console.log(error.name);
}

Output:

SyntaxError

Notes

A syntax error in the program's own code cannot be caught: such code never runs at all. Only errors from parsing data at run time — JSON, regular expressions, new Function() — can be caught.

See also

  • JSON.parse — Parses a JSON string and returns the value.
  • Error — The base error class: created and thrown with throw.

In the tutorial

  • Errors and try...catch — Error handling in JavaScript: try, catch and finally blocks, the error object, the throw statement and built-in error types.

Thanks for reading!

Donate to the site — we work hard for you.