What are the built-in JavaScript error types, and how do you tell them apart?
JavaScript has seven built-in error constructors, and each one signals a different category of mistake.
A TypeError means a value was of the wrong type for the operation. A ReferenceError means the name does not exist in any enclosing scope. A RangeError means the value had the right type but was out of the allowed range, such as an array length that cannot be negative. A SyntaxError means the code could not be parsed, and it is thrown before your code runs at all. A EvalError relates to the eval function and is very rare. A URIError means a malformed URI was passed to the encoding and decoding functions. And an AggregateError was added later to carry several errors at once, and it is what the promise combinators use internally.
All of them extend the base Error type, so The name property is the more reliable of the two, because a custom error class can set its own name.instanceof Error is true for every one of them. To tell them apart you look at the name property, or you check the constructor.
One thing to be clear about. A SyntaxError cannot be caught by a try and catch around the code that contains it, because the parse happens before the code starts running. You can only catch it with a try and catch around an eval call, which is exactly why dynamic evaluation is best avoided in production.