JavaScript, inside out · Chapter 7 of 51

Type coercion

By the end of this chapter you can tell when JavaScript converts a value behind your back, and convert form values yourself so that sums and yes or no answers come out right. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First day at Tapri
  • 4 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Ravi's cart has a quantity box. A customer with one chai in the cart types 2 more and presses Add, and the cart shows a number nobody ordered. Another customer unticks "extra ginger", and the kitchen gets extra ginger anyway. "It is a computer," Meera says. "How can it not add?"

It can add. But everything a form sends arrives as a string, even the digits and the tick box, and the code so far uses those strings as if they were numbers and booleans. When an operator meets two different types, JavaScript quietly converts one of them. That is called coercion, and it does not always go the way you expect.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    addToCart: fix the cart that joins instead of adding

    Fix addToCart(cartQty, typed). cartQty is the number already in the cart, and typed is what the customer typed in the quantity box: always a string of digits, like '2'. Return the new quantity as a number: addToCart(1, '2') gives 3, and addToCart(5, '10') gives 15.

    It is broken now: it gives back text instead of the sum.

  2. Task 2

    readForm: real numbers and booleans from the form

    Write readForm(form). The form has three fields, and every one is a string: qty, tableNumber and extraGinger. Return a new object with the same three keys: qty and tableNumber as numbers, and extraGinger as a boolean, true when the field is the word 'true' and false when it is the word 'false'.

    readForm({ qty: '2', tableNumber: '07', extraGinger: 'false' }) gives { qty: 2, tableNumber: 7, extraGinger: false }, and a table number with a zero in front, like '07', is just 7.

  3. Task 3

    arithmeticPredictions: what mixed-type sums give

    Export arithmeticPredictions, an array of seven values: what each expression in the comment above it gives, in the same order. The type counts as well as the value: write a string answer as a string in quotes, and a number answer as a number without quotes.

    The check works out the same seven expressions and compares.

  4. Task 4 · interview question

    Interview: looseEqualityPredictions, what == really says

    Export looseEqualityPredictions, an array of eight booleans: true or false for each comparison in the comment above it, in the same order.

    The check works out the same eight comparisons and compares.

What you will learn

  • Explicit conversion: Number and String, on purpose.
  • Implicit conversion: what +, the other operators and comparisons do with mixed types.
  • == and ===: which one converts before comparing.

More in First day at Tapri

Build "Type coercion" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.