W3cubDocs

/JavaScript

Error.error

The Error constructor creates an error object.

Syntax

new Error([message[, fileName[, lineNumber]]])

Parameters

messageOptional
A human-readable description of the error.
fileName Optional
The value for the fileName property on the created Error object. Defaults to the name of the file containing the code that called the Error() constructor.
lineNumber Optional
The value for the lineNumber property on the created Error object. Defaults to the line number containing the Error() constructor invocation.

Examples

Function call or new construction

When Error is used like a function -- without new, it will return an Error object. Therefore, a mere call to Error will produce the same output that constructing an Error object via the new keyword would.

// this...
const x = Error('I was created using a function call!')

​​​​// ...has the same functionality as this.
const y = new Error('I was constructed via the "new" keyword!')

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Error() constructor 1 12 1 6 4 1
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Error() constructor 1 18 4 10.1 1 1.0
Server
Node.js
Error() constructor 0.1.100

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error