When a number looks wrong, I want to see the values that reached the calculation before changing the code. This small example uses a breakpoint to find an input-type bug.

Use Chrome DevTools on a local page you control. Open Sources, create a new Snippet, paste the example, and run it. The debugger statement pauses execution when DevTools is open.

1. Reproduce a specific wrong result

The expected total is 25. Run the snippet and inspect its result. A form value is represented here by the string "20", while shipping is a number.

function calculateTotal(price, shipping) {
  debugger;
  return price + shipping;
}

const total = calculateTotal("20", 5);
console.log(total); // "205", not 25

2. Inspect the inputs while paused

Select the calculateTotal call frame. In the Console, evaluate typeof price and typeof shipping. You should see string and number. Step over the return or resume to observe how the result is produced.

For a larger application, set a line breakpoint near the operation that produces the wrong result. Use the call stack to trace where its inputs came from. A conditional breakpoint is useful when the function runs frequently but only one input fails.

typeof price     // "string"
typeof shipping  // "number"
price + shipping // "205"

3. Convert and validate at the boundary

In this exercise, price must be a non-empty string representing a finite non-negative number. Reject missing input before conversion, since Number("") becomes zero. Validate shipping too, rather than assuming every caller passes a usable number.

The result still uses ordinary JavaScript numbers. For money in production, choose a deliberate representation and rounding policy; this example is about diagnosing types, not implementing billing.

function calculateTotal(rawPrice, shipping) {
  if (typeof rawPrice !== "string" || rawPrice.trim() === "") {
    throw new Error("Price is required");
  }
  const price = Number(rawPrice);
  if (!Number.isFinite(price) || price < 0 ||
      !Number.isFinite(shipping) || shipping < 0) {
    throw new Error("Price and shipping must be valid amounts");
  }
  return price + shipping;
}

4. Check the fix and its edge cases

Replace the original function with the corrected version. The valid case should return 25, and each invalid case below should enter the catch block. Keep a focused regression check in the project once the bug is fixed.

Remove the temporary debugger statement before shipping. In real code, translate the validation failure into useful feedback at the form or API boundary.

console.assert(calculateTotal("20", 5) === 25);
for (const input of ["", "hello", "-1"]) {
  try {
    calculateTotal(input, 5);
    console.error("Expected rejection:", input);
  } catch (error) {
    console.log("Rejected as expected:", input);
  }
}

Further reading