JavaScript Error message Property

The message property of JavaScript Error objects contains a human-readable description of the error. It's automatically set when an error occurs and can also be customized when creating custom errors.

Syntax

error.message

Example: Accessing Error Message






JavaScript Error message Property






Click the button to see the error message

Creating Custom Error Messages




Custom Error Messages







Common Error Types and Messages

Error Type Example Message Cause
ReferenceError "variable is not defined" Using undefined variables
TypeError "Cannot read property 'x' of null" Accessing properties on null/undefined
SyntaxError "Unexpected token" Invalid JavaScript syntax
Custom Error Your custom message Thrown with new Error()

Key Points

  • The message property is read/write - you can modify it after creating an error
  • Different browsers may format error messages slightly differently
  • Custom error messages should be descriptive and user-friendly
  • The message property doesn't include the error type - use error.name for that

Conclusion

The message property provides essential error information for debugging and user feedback. Use it in try-catch blocks to handle errors gracefully and create meaningful custom error messages.

Updated on: 2026-03-15T23:18:59+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements