JavaScript Foundations·Build and ship

Errors and debugging

Read a stack trace, use the console deliberately and prevent silent failures.

Beginner 12 min read Lesson 13 of 480 Challenge included
What you will be able to do
  • Distinguish error types
  • Read a stack trace
  • Validate at boundaries

Bugs are information. A clear error message with a stack trace tells you exactly where execution stopped. The skill is reading it calmly.

ErrorUsually means
SyntaxErrorThe code could not be parsed at all.
ReferenceErrorYou used a name that does not exist in scope.
TypeErrorYou called or accessed something on the wrong type.
RangeErrorA value is outside the allowed range.
validate.jsJavaScript
function parseScore(raw) {
  const score = Number(raw);

  if (!Number.isFinite(score)) {
    throw new TypeError(`Invalid score: ${raw}`);
  }

  if (score < 0 || score > 100) {
    throw new RangeError("Score must be between 0 and 100");
  }

  return score;
}
Your turn

Guard the input

Easy

Throw a TypeError if email does not include "@", otherwise return the trimmed email.

JAVASCRIPT · starter
function validateEmail(email) {
  // your code here
}
Check your understanding before moving on.
Knowledge checkQuestion 1

What does a ReferenceError usually indicate?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Modules and tooling
Lesson 13 of 480 · 0% of course