Skip to content

Latest commit

 

History

History
84 lines (67 loc) · 2.79 KB

File metadata and controls

84 lines (67 loc) · 2.79 KB
title slug tags browser-compat
SyntaxError
Web/JavaScript/Reference/Global_Objects/SyntaxError
Class
JavaScript
Object
Reference
SyntaxError
javascript.builtins.SyntaxError

{{JSRef}}

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 {{Glossary("serializable object")}}, so it can be cloned with {{domxref("structuredClone()")}} or copied between Workers using {{domxref("Worker.postMessage()", "postMessage()")}}.

Constructor

  • {{jsxref("Global_Objects/SyntaxError/SyntaxError", "SyntaxError()")}}
    • : Creates a new SyntaxError object.

Instance properties

  • {{jsxref("Error.prototype.message", "SyntaxError.prototype.message")}}
    • : Error message. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.name", "SyntaxError.prototype.name")}}
    • : Error name. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.cause", "SyntaxError.prototype.cause")}}
    • : Error cause. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.fileName", "SyntaxError.prototype.fileName")}} {{non-standard_inline}}
    • : Path to file that raised this error. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.lineNumber", "SyntaxError.prototype.lineNumber")}} {{non-standard_inline}}
    • : Line number in file that raised this error. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.columnNumber", "SyntaxError.prototype.columnNumber")}} {{non-standard_inline}}
    • : Column number in line that raised this error. Inherited from {{jsxref("Error")}}.
  • {{jsxref("Error.prototype.stack", "SyntaxError.prototype.stack")}} {{non-standard_inline}}
    • : Stack trace. Inherited from {{jsxref("Error")}}.

Examples

Catching a SyntaxError

try {
  eval('hoo bar');
} catch (e) {
  console.error(e instanceof SyntaxError);
  console.error(e.message);
  console.error(e.name);
  console.error(e.fileName);
  console.error(e.lineNumber);
  console.error(e.columnNumber);
  console.error(e.stack);
}

Creating a SyntaxError

try {
  throw new SyntaxError('Hello', 'someFile.js', 10);
} catch (e) {
  console.error(e instanceof SyntaxError); // true
  console.error(e.message);                // Hello
  console.error(e.name);                   // SyntaxError
  console.error(e.fileName);               // someFile.js
  console.error(e.lineNumber);             // 10
  console.error(e.columnNumber);           // 0
  console.error(e.stack);                  // @debugger eval code:3:9
}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{jsxref("Error")}}