

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Error Object
incomplete
2: Handling Errors
incomplete
3: Finally
incomplete
4: Throwing Errors
incomplete
5: When to try/catch
incomplete
This lesson's interactive features are locked, please to keep using them
Errors in JavaScript, like Go, are called Errors. It sure beats a silly name like "Exceptions"...
The most important property on the built-in error object is the message property: which should be a human-readable description of the error.
const err = new Error("We've run out of baked salmon");
console.log(err.message);
// We've run out of baked salmon
Error messages at Textio have Error: in front of them. Convenient, right? Wrong. The devs keep forgetting to add it, so upper management has come up with a brilliant plan. A factory function that takes a string and returns a new Error object with 'Error: ' prepended to the message.
For example:
const err = createError("Boots is hungry");
console.log(err.message);
// Error: Boots is hungry
Surely this will solve all of their problems.