QuestenaPractice that shows what to review next
Topic lesson

Common errors: locate the failing operation

About 5 min

Questions stay in the language in which they were published.

These questions ask you to distinguish error types from non-throwing sentinel values and to spot common boundary mistakes. The fastest method is to find the first operation that can fail, then ask whether name lookup succeeded and whether the resulting value supports that operation.

Classify the first failing step

SituationResult and reason
A name cannot be resolvedOrdinary evaluation throws a ReferenceError. This includes reading a block-scoped name after its block when no accessible outer binding has that name.
A value cannot perform the requested operationThe operation throws a TypeError. Examples include calling a number, reading a property directly through null or undefined, rebinding an initialized const, and directly mixing BigInt with Number in arithmetic.
An empty reduction has no starting accumulatorreduce without an initial value throws a TypeError because there is no element from which to begin. Supplying an initial value makes that value the empty-array result.
An array position is absentA normal read produces undefined rather than an exception. The index equal to array length is just beyond the end because the final valid index is one less than length.
Text has no numeric interpretationNumber conversion produces NaN without throwing. Number.isNaN checks whether a value is already the Number value NaN and does not convert strings or undefined first.

Treat NaN as a special Number value, not as an exception. It is unequal to itself under both equality operators, and ordered comparisons involving it are false. Use Number.isNaN for a direct non-coercing test, or Object.is when that comparison is appropriate, rather than comparing a value to NaN with equality.

Where the mistakes come from

Tempting routeHow to reject it
Calling every failed operation a ReferenceErrorFirst resolve the name. If a value is found but is not callable, the failure concerns the value's type and capabilities, not lexical lookup.
Assuming const freezes nested data, mixed numeric arithmetic converts automatically, or empty reduce invents a starting valueSeparate binding reassignment from object mutation, make numeric types agree explicitly, and provide an accumulator when an empty input must be handled.
Expecting an inaccessible block name to become undefinedUndefined is a value; failed lexical lookup is an error. Once the block binding is out of scope, another accessible declaration must exist or evaluation throws a ReferenceError.
Treating length as the last array indexZero-based indexes stop one position earlier. A loop that permits the index to equal length performs an extra pass, while merely reading that absent position produces undefined.
Expecting equality with NaN to behave like equality with an ordinary NumberNaN does not equal itself, and ordered comparisons with it are false. Use a direct NaN test instead of an equality shortcut.

Try it

Question 109

The variable target is undefined. What error type results from evaluating target.name directly?

  1. TypeError
  2. ReferenceError
  3. SyntaxError
  4. No error; the result is undefined
Question 118

Which statements describe Number("hello")? Select all that apply.

  1. The result is undefined
  2. The result is NaN
  3. The result is zero
  4. The conversion does not throw an exception
Question 119

Which call returns true under the non-coercing rules of Number.isNaN?

  1. Number.isNaN("hello")
  2. Number.isNaN(undefined)
  3. Number.isNaN(NaN)
  4. Number.isNaN("NaN")
Start drillPractice this topic
Common errors: locate the failing operation · Questena