What properties does an error object have?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Medium
#JavaScript #JS Basics

Brief Answer

Error object in JavaScript has several standard properties that help identify and debug errors. Main properties: name (error type), message (error message) and stack (call stack). These properties allow efficient error handling, logging and analysis in applications.

Main properties:

  • name — error type (e.g., “Error”, “TypeError”)
  • message — error message text
  • stack — call stack at the time of error occurrence

Full Answer

Error object in JavaScript is a special object created when an exception occurs. It contains information needed to identify, debug and handle errors. Understanding error object properties is critically important for effective exception handling.

Main Error Object Properties

1. name

The name property contains a string with the error type:

try {
  throw new Error('Test error');
} catch (error) {
  console.log(error.name); // "Error"
}

2. message

The message property contains the error description text:

try {
  throw new Error('Something went wrong');
} catch (error) {
  console.log(error.message); // "Something went wrong"
}

3. stack

The stack property contains call stack information:

try {
  throw new Error('Error with traceback');
} catch (error) {
  console.log(error.stack); // Call stack
}

Error Types and Their Properties

JavaScript provides various error types, each with its own name:

  • Error — base error type
  • SyntaxError — syntax error
  • ReferenceError — reference error
  • TypeError — type error
  • RangeError — range error
  • URIError — URI error
  • EvalError — eval error

Working with Error Properties

Getting Error Information

function handleError(error) {
  console.log(`Type: ${error.name}`);
  console.log(`Message: ${error.message}`);
  console.log(`Stack: ${error.stack}`);
}

Checking Error Type

try {
  // ... code that might cause an error
} catch (error) {
  if (error instanceof TypeError) {
    console.log('Type error:', error.message);
  } else if (error instanceof ReferenceError) {
    console.log('Reference error:', error.message);
  }
}

Practical Examples

Error Logging

Error properties are useful for logging:

function logError(error) {
  const errorInfo = {
    type: error.name,
    message: error.message,
    stack: error.stack,
    timestamp: new Date().toISOString()
  };
  
  console.error('An error occurred:', errorInfo);
}

Creating Custom Errors

You can create errors with additional properties:

class CustomError extends Error {
  constructor(message, code) {
    super(message);
    this.name = 'CustomError';
    this.code = code; // Additional property
  }
}
 
const error = new CustomError('Custom error', 'ERR_001');
console.log(error.code); // "ERR_001"

Common Mistakes and Solutions

Incorrect Error Type Checking

// ❌ Incorrect
if (error.name === 'TypeError') {
  // ...
}
 
// ✅ Correct
if (error instanceof TypeError) {
  // ...
}

Ignoring Call Stack

The stack property is critically important for debugging and should not be ignored.

Best Practices

  1. Always check error type — use instanceof
  2. Log all error properties — for effective debugging
  3. Create custom errors — for specific cases
  4. Don’t ignore call stack — it helps find error source
  5. Use clear messages — message should be informative

Key Benefits of Knowing Error Properties

  1. Effective debugging — call stack shows error path
  2. Proper handling — different error types allow different responses
  3. Better logging — complete error information
  4. Improved UX — ability to show user-friendly messages

Understanding error object properties is the foundation of effective exception handling in JavaScript. This knowledge helps create more reliable and debuggable applications.


Knowledge Check Task

Task

What properties will the error object have in this example and what will they contain?

try {
  const obj = null;
  console.log(obj.property);
} catch (error) {
  // What will be the values of error.name, error.message, error.stack?
}
View answer

Answer: The error object will have the following properties:

  1. error.name — “TypeError” Error occurs when trying to access property of null

  2. error.message — “Cannot read property ‘property’ of null” (exact text may vary depending on browser)

  3. error.stack — Call stack showing path to error Contains information about which line and file the error occurred

Explanation:

  • TypeError occurs when trying to perform an operation on a variable of inappropriate type
  • In this case we’re trying to get a property of null, which is impossible
  • Call stack shows the exact location of the error, which helps in debugging

Understanding these properties allows:

  • Proper identification of error type
  • Showing appropriate message to user
  • Logging error with complete information for debugging

Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪