R
Rishtaara
JavaScript Fundamentals
Lesson 14 of 28Article16 min

Errors, Debugging, Style & Versions

try runs risky code. catch handles errors. finally always runs — good for cleanup.

try, catch, finally

try runs risky code. catch handles errors. finally always runs — good for cleanup.

Real-life example: try — cook carefully. catch — if milk spills, wipe floor. finally — turn off gas either way.

Error handling
try {
  const data = JSON.parse('{"ok":true}');
  console.log(data.ok);
} catch (err) {
  console.log("Bad JSON:", err.message);
} finally {
  console.log("Done trying");
}

throw

throw creates your own error when something is wrong — bad input, missing field.

Real-life example: throw — refuse to serve if the order has no item name.

Throw custom error
function divide(a, b) {
  if (b === 0) {
    throw new Error("Cannot divide by zero");
  }
  return a / b;
}

Debugging with console

Use console.log, console.table, and console.error. Read red error messages — they show file and line number.

Real-life example: console.log is leaving breadcrumbs on a path so you know where you walked.

Console helpers
const users = [{ name: "A" }, { name: "B" }];
console.table(users);
console.error("Something went wrong");

Breakpoints (idea)

In DevTools (F12 → Sources), click a line number to pause code there. Step line by line and inspect variables.

Real-life example: Pause a movie frame by frame to see exactly what happened before the bug.

  • F12 → Sources tab → open your .js file
  • Click line number for blue breakpoint
  • Refresh page — code stops at that line
  • Use Step Over to go line by line

Style Guide — naming, semicolons, const-first

Use clear camelCase names. End statements with semicolons for safety. Prefer const, then let. Use 2-space indent.

Real-life example: Clean code is a neat shop shelf — customers (and you next month) find things fast.

Clean style
const MAX_USERS = 100; // constants often UPPER_SNAKE
const userName = "asha"; // camelCase for variables

function getTotal(items) {
  return items.reduce((sum, x) => sum + x, 0);
}

JavaScript Versions — ES5 and ES6+

ES5 (2009) — older baseline. ES6 / ES2015+ — let/const, arrow functions, classes, modules, and more. Modern browsers support ES6+ well.

Real-life example: ES5 is an old textbook edition. ES6+ is the updated book with clearer chapters — learn the new edition.

  • ES5: var, function, classic syntax
  • ES6+: let, const, arrow, template literals, destructuring
  • New features arrive yearly (ES2017, ES2020 with ?? and ?.)

Reference — MDN

MDN Web Docs (developer.mozilla.org) is the best free reference for JavaScript. Search any method — Array.map, Date, fetch.

Real-life example: MDN is a dictionary for programmers — look up any word (function) when you forget spelling or meaning.

Bookmark MDN for JavaScript and keep this Rishtaara course for guided learning in simple English.