Error() constructor

The Error constructor creates an error object.

Syntax

new Error()
new Error(message)
new Error(message, fileName)
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 Language Specification (ECMAScript)
# sec-error-constructor

Browser compatibility

BCD tables only load in the browser

See also