JavaScript, inside out · Chapter 34 of 51

Optional chaining and nullish

By the end of this chapter you can read deep into data that may be missing without crashing, and fill in defaults without wiping out a real 0 or an empty string. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Modern JavaScript
  • 5 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

On Saturday at 4 pm the rider screen went white. A walk-in customer had picked up their chai at the counter, so the order had no delivery, and the code that finds the rider's city stopped with TypeError: Cannot read properties of undefined (reading 'address'). The same afternoon a customer who asked for no sugar got two spoons: the preferences screen saved sugar: 0, and the kitchen code read it with ||. Ravi also wants missing order details filled in, and Anjali wants coupon codes tidied without inventing coupons nobody entered.

With what you know so far, reading safely means asking at every level: "is there a delivery? is there an address?", with && or an if per step, and one forgotten step crashes the screen. Defaults with || replace every falsy value, 0 and '' included. And setting a default on an object means writing the key out twice.

What you build: 5 tasks

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

  1. Task 1

    riderCity: the delivery city without crashing

    Fix riderCity(order). It returns order.delivery.address.city, the city the rider is heading to: for { delivery: { address: { city: 'Bengaluru' } } } it gives 'Bengaluru'.

    It is broken now: it throws when a piece is missing. It must give undefined, without throwing, when the order itself is null or undefined, when there is no delivery (a pickup order) or delivery is null, and when there is no address or address is null.

  2. Task 2

    firstItemName and sendReceipt: a safe index and a safe call

    Write firstItemName(order). Return the name of the first item in order.items, or undefined when the items list is empty, missing or null. The order itself is always an object.

    Write sendReceipt(customer, text). When the customer has a notify method, call it with text and return whatever it returns. Return undefined, without throwing, when customer is null or undefined, has no notify, or has notify set to null.

  3. Task 3 · interview question

    Interview: orderDefaults, keep 0 sugar and an empty note

    Fix orderDefaults(prefs). It returns a new object with three keys, sugar, note and cups. Each one is the customer's value, or a default when the value is null or undefined: sugar 2, note 'none', cups 1.

    It is broken now: it also replaces 0 and '', so a customer who asked for no sugar gets two spoons. { sugar: 0, note: '', cups: 3 } must give back exactly those values, and {} gives { sugar: 2, note: 'none', cups: 1 }.

  4. Task 4

    prepareOrder: fill in the missing details

    Write prepareOrder(order). Change the order you are given and return that same object (not a copy). tableNo becomes 'takeaway' only when it is null or undefined (or missing): table 0 is the counter seat and stays 0. customerName becomes 'Guest' when it is missing or empty. Every other key stays as it is, and a real name such as 'Meera' is kept.

    For { items: ['cutting chai'] } the order becomes { items: ['cutting chai'], tableNo: 'takeaway', customerName: 'Guest' }.

  5. Task 5

    tidyCoupon: tidy the coupon only when there is one

    Write tidyCoupon(order). When the order has a coupon, trim the spaces around it and make it upper case, on the order itself: ' monsoon10 ' becomes 'MONSOON10'. Return that same object.

    When the order has no coupon, leave it alone: no coupon key may be added ('coupon' in order stays false). An empty coupon '' stays ''.

What you will learn

  • Optional chaining ?.: stop at the first missing piece and give undefined, for keys, brackets and calls.
  • ?? against ||: which one keeps a real 0 or '', and the rule about mixing them.
  • ??=, ||= and &&=: assign only when it is needed.

More in Modern JavaScript

Build "Optional chaining and nullish" 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.