The Error
constructor creates an error object.
Syntax
new Error([message[, fileName[, lineNumber]]])
Parameters
message
Optional- A human-readable description of the error.
fileName
Optional- The value for the
fileName
property on the createdError
object. Defaults to the name of the file containing the code that called theError()
constructor. lineNumber
Optional- The value for the
lineNumber
property on the createdError
object. Defaults to the line number containing theError()
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
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Error constructor' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Error() constructor | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 6 | Opera Full support 4 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
Legend
- Full support
- Full support