SyntaxError - JavaScript - MDN Web Docs - Mozilla

  • Skip to main content
  • Skip to search
SyntaxError Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

  • Learn more
  • See full compatibility
  • Report feedback

The SyntaxError object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.

SyntaxError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage().

SyntaxError is a subclass of Error.

In this article

  • Constructor
  • Instance properties
  • Instance methods
  • Examples
  • Specifications
  • Browser compatibility
  • See also

Constructor

SyntaxError()

Creates a new SyntaxError object.

Instance properties

Also inherits instance properties from its parent Error.

These properties are defined on SyntaxError.prototype and shared by all SyntaxError instances.

SyntaxError.prototype.constructor

The constructor function that created the instance object. For SyntaxError instances, the initial value is the SyntaxError constructor.

SyntaxError.prototype.name

Represents the name for the type of error. For SyntaxError.prototype.name, the initial value is "SyntaxError".

Instance methods

Inherits instance methods from its parent Error.

Examples

Catching a SyntaxError

jstry { eval("hoo bar"); } catch (e) { console.log(e instanceof SyntaxError); // true console.log(e.message); console.log(e.name); // "SyntaxError" console.log(e.stack); // Stack of the error }

Creating a SyntaxError

jstry { throw new SyntaxError("Hello"); } catch (e) { console.log(e instanceof SyntaxError); // true console.log(e.message); // "Hello" console.log(e.name); // "SyntaxError" console.log(e.stack); // Stack of the error }

Specifications

Specification
ECMAScript® 2026 Language Specification# sec-native-error-types-used-in-this-standard-syntaxerror

Browser compatibility

See also

  • Error

Help improve MDN

Was this page helpful to you? Yes No Learn how to contribute

This page was last modified on Jul 10, 2025 by MDN contributors.

View this page on GitHub • Report a problem with this content Filter sidebar
  1. Standard built-in objects
  2. SyntaxError
  3. Constructor
    1. SyntaxError()
  4. Inheritance
  5. Error
  6. Constructor
    1. Error()
  7. Static methods
    1. captureStackTrace()
    2. isError()
  8. Static properties
    1. stackTraceLimit Non-standard
  9. Instance methods
    1. toString()
  10. Instance properties
    1. cause
    2. columnNumber Non-standard
    3. fileName Non-standard
    4. lineNumber Non-standard
    5. message
    6. name
    7. stack Non-standard
  11. Object/Function
  12. Static methods
    1. apply()
    2. bind()
    3. call()
    4. toString()
    5. [Symbol.hasInstance]()
  13. Static properties
    1. displayName Non-standard
    2. length
    3. name
    4. prototype
    5. arguments Non-standard Deprecated
    6. caller Non-standard Deprecated
  14. Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. hasOwnProperty()
    6. isPrototypeOf()
    7. propertyIsEnumerable()
    8. toLocaleString()
    9. toString()
    10. valueOf()
  15. Instance properties
    1. __proto__ Deprecated
    2. constructor
  16. Related pages
  17. AggregateError
  18. EvalError
  19. InternalError Non-standard
  20. RangeError
  21. ReferenceError
  22. TypeError
  23. URIError

Tag » What Is A Syntax Error