Course contentsShow
← JavaScript
Lesson 1148 of 2,72326. Error HandlingFree lesson

Error Object Properties

Examine standard and non-standard properties like name, message, fileName, and lineNumber.

Error Object Properties

You'll learn which properties Error objects carry and how to use them for debugging and error handling.

When you create or catch an error in JavaScript, you're working with an Error object that contains several properties describing what went wrong. These properties help you understand, log, and handle errors effectively.

Standard Properties

Every Error object has two guaranteed standard properties:

  • name: A string indicating the error type (e.g., "TypeError", "ReferenceError", "Error")
  • message: A human-readable description of what went wrong
try {
  throw new TypeError("Expected a number");
} catch (err) {
  console.log(err.name);     // "TypeError"
  console.log(err.message);  // "Expected a number"
}

These are the only properties guaranteed by the ECMAScript specification to exist on all Error objects across all JavaScript environments.

Non-Standard but Common Properties

Most JavaScript engines also provide additional properties that are incredibly useful but not officially standardized:

  • stack: A string containing the call stack trace (you've seen this before!)
  • fileName: The file where the error occurred (Mozilla-specific)
  • lineNumber: The line number where the error occurred (Mozilla-specific)
  • columnNumber: The column number (Mozilla-specific)
try {
  undefined.foo();
} catch (err) {
  console.log(err.stack);  // Multi-line stack trace
  // fileName, lineNumber available in Firefox/SpiderMonkey
}

Because fileName and lineNumber aren't universal, rely on stack for cross-platform debugging—it typically contains file and line information in its formatted output.

Key Takeaway: Error objects always have name and message properties; use these for portable error handling, and leverage stack for debugging across environments.