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:
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.
The name property contains a string with the error type:
try {
throw new Error('Test error');
} catch (error) {
console.log(error.name); // "Error"
}The message property contains the error description text:
try {
throw new Error('Something went wrong');
} catch (error) {
console.log(error.message); // "Something went wrong"
}The stack property contains call stack information:
try {
throw new Error('Error with traceback');
} catch (error) {
console.log(error.stack); // Call stack
}JavaScript provides various error types, each with its own name:
function handleError(error) {
console.log(`Type: ${error.name}`);
console.log(`Message: ${error.message}`);
console.log(`Stack: ${error.stack}`);
}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);
}
}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);
}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"// ❌ Incorrect
if (error.name === 'TypeError') {
// ...
}
// ✅ Correct
if (error instanceof TypeError) {
// ...
}The stack property is critically important for debugging and should not be ignored.
Understanding error object properties is the foundation of effective exception handling in JavaScript. This knowledge helps create more reliable and debuggable applications.
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?
}Answer: The error object will have the following properties:
error.name — “TypeError” Error occurs when trying to access property of null
error.message — “Cannot read property ‘property’ of null” (exact text may vary depending on browser)
error.stack — Call stack showing path to error Contains information about which line and file the error occurred
Explanation:
Understanding these properties allows:
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪