Error() constructor

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

Specification
ECMAScript (ECMA-262)
The definition of 'Error constructor' in that specification.

Browser compatibility

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Error() constructorChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 6Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support
Full support

See also