#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

JavaScript Errors

A JavaScript error is an object the engine creates when something goes wrong while your code is being read or run, such as using a variable that does not exist or calling something that is not a function. Errors stop the current script, print a message in the console, and, once you understand their types, tell you exactly where to look.

Reading an error message

Every error has a name, a message and usually a stack trace. In the console, you might see:

Uncaught TypeError: Cannot read properties of undefined (reading 'name')
    at showUser (app.js:14:22)
    at app.js:20:1

Read it from left to right. TypeError is the kind of problem. The message says something was undefined when you tried to read .name from it. The stack trace lists where it happened: inside showUser, in app.js, line 14, column 22, which was called from line 20. Click the file link in developer tools to jump straight there.

The built-in error types

TypeWhen it happensExample cause
SyntaxErrorCode cannot be parsedMissing bracket, or JSON.parse('{bad}')
ReferenceErrorUsing a name that is not definedTypo like consol.log, or reading a let before its line
TypeErrorA value is the wrong type for the operationCalling undefined() or reading a property of null
RangeErrorA number is outside the allowed rangenew Array(-1), or endless recursion
URIErrorBad input to URI functionsdecodeURIComponent('%')

All of them inherit from the general Error type, which you can also use yourself.

Syntax errors versus runtime errors

A syntax error is found before the script runs, so none of that file's code executes. That is why a single missing brace can make an entire page "do nothing". Runtime errors, like TypeError, happen while the code is running; everything before that line already ran, and the rest of that call stops.

Throwing your own errors

You can signal problems in your own functions with throw. Always throw an Error object rather than a plain string, because only error objects carry a stack trace that shows where the problem started.

function withdraw(balance, amount) {
  if (typeof amount !== 'number' || amount <= 0) {
    throw new TypeError('Amount must be a positive number');
  }
  if (amount > balance) {
    throw new RangeError('Insufficient balance');
  }
  return balance - amount;
}

try {
  withdraw(500, 800);
} catch (err) {
  console.log(err.name);     // "RangeError"
  console.log(err.message);  // "Insufficient balance"
}

The try and catch blocks here are a preview; the next lesson explains error handling mechanisms properly.

The most common error and how to prevent it

"Cannot read properties of undefined" is probably the error beginners meet most. It usually means data was not there when you expected it: an API response with a different shape, an array index past the end, or an element that querySelector did not find. Log the value just before the failing line to see what it really is. Optional chaining (user?.name) helps when a value is legitimately allowed to be missing, but do not use it to hide bugs where the value should always exist.

Habits that make errors easier

  • Keep the developer tools console open while you work, so you see errors the moment they happen.
  • Fix the first error in the list first; later ones are often side effects of it.
  • Search the exact message text when you do not recognise it.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course