JavaScript, inside out · Chapter 31 of 51

Symbols

By the end of this chapter you can make values that can never be faked, hide keys from Object.keys and JSON, and plug your own objects into for...of and type conversion. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

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

The order statuses are plain strings, and somewhere in the kitchen code somebody typed 'redy': that order never showed as ready. Then Anjali found a customer receipt that printed the kitchen's private note, "regular, extra strong", because the note was an ordinary key and the receipt prints every key. Kabir also wants the token queue to work with for...of, and the bill to turn itself into a number or a sentence.

With what you know so far, a status is a string anyone can mistype, and every key you add to an object shows in Object.keys, for...in and JSON.stringify. And for...of on your own object simply refuses:

TypeError: queue is not iterable

What you build: 6 tasks

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

  1. Task 1

    Status and statusLabel: statuses nobody can fake

    Make Status.PLACED, Status.BREWING and Status.READY three different symbols, with the descriptions 'placed', 'brewing' and 'ready'. Keep Status frozen.

    Write statusLabel(status). For one of those three symbols it returns its description: statusLabel(Status.BREWING) gives 'brewing'. Anything else gives 'unknown', including a look-alike such as Symbol('ready') and the string 'ready'.

  2. Task 2

    addKitchenNote and kitchenNote: a note receipts never print

    Write addKitchenNote(order, note). Return a new object (not the order you were given) with all of the order's keys and values, plus the note stored under the symbol KITCHEN_NOTE. Its Object.keys and JSON.stringify are the same as the order's, so the note never shows. The original order must not change: no symbol key is added to it.

    Write kitchenNote(order). Return the note stored under KITCHEN_NOTE, or undefined when the order has none.

  3. Task 3

    loyaltyKey and isSharedKey: one symbol for the whole app

    Write loyaltyKey(). Return the symbol registered in the global symbol registry under the key 'tapri.loyalty', so every call, from any file, gives the very same symbol: loyaltyKey() === Symbol.for('tapri.loyalty') is true.

    Write isSharedKey(sym). Return true when the symbol came from the global registry (any key, such as 'tapri.offers') and false for a symbol made with Symbol(), even one with the same description.

  4. Task 4

    makeTokenQueue: a queue for...of and spread can walk

    makeTokenQueue(tokens) returns an object with a tokens key holding the array it was given. Make that object iterable: [...makeTokenQueue([101, 102, 103])] gives [101, 102, 103], and for...of and Array.from give the same tokens in order.

    Every walk starts again from the first token, and a token pushed onto the queue's tokens later is included in the next walk. An empty queue gives [].

  5. Task 5

    makeBill: a bill that becomes a number or a sentence

    Write makeBill(items). Return an object with a total key, the sum of price times qty over the items, and a Symbol.toPrimitive method. For the string hint it gives text like 'Bill: ₹130'; for the number and default hints it gives the total itself, a number.

    For two masala chai at 40 and two bun maska at 25: +bill gives 130, bill > 100 is true, a template literal with the bill and String(bill) both give 'Bill: ₹130', and bill + 10 gives 140.

  6. Task 6 · interview question

    Interview: allKeys, every own key with symbols

    Write allKeys(obj). Return an array of every own key of the object: first its string keys, enumerable or not, in the order JavaScript keeps them, then its symbol keys. Inherited keys are not included.

    For { id: 9, item: 'cutting chai', [tag]: 'vip' } with a hidden secret key added by defineProperty, it gives ['id', 'item', 'secret', tag]. An empty object gives [].

What you will learn

  • Symbol(): a value that is only ever equal to itself.
  • Symbol keys: properties that keys, loops and JSON skip.
  • Symbol.for: one shared symbol for the whole program.
  • Well-known symbols: Symbol.iterator and Symbol.toPrimitive, the hooks JavaScript itself calls.

More in Modern JavaScript

Build "Symbols" 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.