Error() constructor
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
Browser compatibility
BCD tables only load in the browser