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.

Description

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 Latest Draft (ECMA-262)
The definition of 'Error constructor' in that specification.

Browser compatibility

Update compatibility data on GitHub
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 YesSafari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes

Legend

Full support  
Full support

See also