ЗаданиеThrow a new TypeError('expected number') inside a function and catch it. Log err.name, err.message. Also log err.stack.length > 0 to confirm a stack is present (true/false).
The Error object — name, message, stack
75 XP6 min
Теория
What's inside an Error
Every Error has three useful fields:
err.name— the class name ("Error","TypeError","RangeError", custom).err.message— the first arg you passed tonew.err.stack— a multi-line string with the call stack at throw time.
const err = new TypeError("expected number");
err.name; // "TypeError"
err.message; // "expected number"
err.stack; // "TypeError: expected number\n at <anonymous>:1:13..."Built-in error subclasses
JavaScript ships several subclasses you should reach for instead of bare Error:
TypeError— wrong type (e.g. called(undefined).foo).RangeError— number/string outside valid range.SyntaxError— thrown byJSON.parseon bad input.URIError—decodeURIComponenton bad input.
Using the right subclass lets call sites narrow with instanceof (see C7-05).
🔒
Зарегистрируйся чтобы кодить
Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.
Регистрация — бесплатный trial →Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.