liveBook · Manning

Data conversion

As explained in JavaScript in Depth

The process of changing a value from one type to another according to language rules.

A payment check such as `if (!transactionId)` can treat numeric zero as missing and charge a customer twice. JavaScript silently converts values in conditions, arithmetic, comparisons, templates, and built-in methods, so the visible bug may appear far from the conversion that caused it. section 4.0 section 4.4.2

Conversion follows operator-specific rules rather than one universal cast. Minus, multiplication, and division convert operands to numbers; plus prefers string concatenation; loose equality converts aggressively; strict equality performs no conversion. Conditions use truthy and falsy rules, while template literals convert interpolated values to strings. section 4.4.2 section 4.2.1

Useful rules for predicting JavaScript data conversion include:

  • Treat `undefined`, `null`, `false`, numeric zero, `NaN`, and the empty string as falsy; treat strings such as `'0'`, arrays, and objects as truthy. section 4.2.1
  • Expect `'5' - 2` to produce `3`, but `'5' + 2` to produce `'52'`. section 4.4.2
  • Use strict equality when implicit conversion is undesirable; `5 == '5'` is true, while strict equality never coerces. section 4.4.2
  • Convert values once at system boundaries instead of repeatedly inside loops, comparisons, or sorting operations. section 4.4.4 section 4.6

Arrays reveal multi-step conversion

`Number([1])` produces `1`, whereas `Number([1, 2])` produces `NaN`. The array is first converted to a string: `[1]` becomes `'1'`, while `[1, 2]` becomes a multi-value string that cannot become a number. section 4.4.4

function processPayment(amount, discountCode, isPriority) {
  if (!discountCode) {
    discountCode = 'NONE';
  }
  
  const finalAmount = amount - (discountCode * 0.01 * amount);
  
  if (isPriority == true) {
    return expeditedCharge(finalAmount);
  }
  return standardCharge(finalAmount);
}
 
// All broken in different ways:
processPayment(100, '0', false);  #A
processPayment(100, '', false);  #B
processPayment(100, '50', 'yes');  #C

Inspect the payment function to see three independent conversion failures: falsy handling, arithmetic coercion, and loose equality with a non-boolean string. section 4.0

How JavaScript in Depth covers it

How deep it goes
Works through operator-specific rules, edge cases, performance costs, and V8 representation, but defers complete conversion algorithms.
Examples
Uses payment validation, discount codes, arithmetic tables, comparison tables, sorting timestamps, and array-to-number conversion.
What it assumes
Assumes JavaScript primitive values and operators while teaching undefined, null, truthiness, equality, and implicit conversion behavior.
What you can do after
Diagnose coercion bugs, predict operator results, choose strict comparisons, and reduce repeated conversions in performance-sensitive code.
How it connects
Connects data conversion with truthy and falsy values, equality operators, primitive types, performance optimization, and TypeScript.

Also explained in

Compare how 22 books explain data conversion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.