JavaScript, inside out · Chapter 3 of 51
Data types
By the end of this chapter you can tell what type a value is, and copy or compare objects without a change to one leaking into another. 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
- 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
Anjali wants a "repeat my last order" button. Ravi built it in ten minutes, and by lunch a customer who added a bun maska to her repeat order had also changed yesterday's order in the records. Ravi also needs to name the type of any value a form sends, to tell when two orders ask for the same thing, and to keep a stock snapshot at closing time that the next day's changes cannot touch.
So far you have used = to keep a value under a name, and for a number that makes a copy. For an object it does not: const repeat = lastOrder gives the same order a second name, so changing the repeat changes the original too.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
describeValue: the type of any value
Write describeValue(value). It returns the type of the value as text, like typeof, with two differences: for null it returns 'null', and for any array, an empty one too, it returns 'array'.
For every other value (a string, a number, true or false, undefined, a plain object, a function, a bigint) return exactly what typeof gives for it.
- Task 2 · interview question
Interview: typeofPredictions, what typeof really says
Export typeofPredictions, an array of eight strings: what each typeof in the comment above it returns, in the same order as the list.
The check runs the same eight typeof expressions and compares.
- Task 3
repeatOrder: a real copy with the next id
Write repeatOrder(order). Return a new order object with the same customer, the same items and the next id (the order's id plus 1). For { id: 7, customer: 'Anjali', items: ['masala chai'] } it returns { id: 8, customer: 'Anjali', items: ['masala chai'] }.
The copy must share nothing with the original: it is a different object, its items is a different array, and changing the copy's customer or adding to its items leaves the original order exactly as it was.
- Task 4
isSameOrder: same item and same qty
Write isSameOrder(a, b). Return true when both orders are for the same item and the same qty, even when they are two different objects, and false otherwise.
The qty must match exactly: a qty of 2 and a qty of '2' (text) are not the same. The same object passed twice is the same order.
- Task 5
snapshotStock: a deep copy of the stock
Write snapshotStock(stock). Return a copy of the stock that shares nothing with it at any level. The stock is an object of objects, like { masala: { kulhads: 50, ginger: 12 } }.
After the snapshot is taken, changing a count inside the stock, or adding a new shelf to it, must not change the snapshot.
What you will learn
- Primitives and objects: the seven simple types, and everything else.
- typeof, Array.isArray and === null: asking a value for its type.
- Values and references: why = copies a number but shares an object, and what === really compares.
- Copies: spread for one level, structuredClone for every level.
More in First day at Tapri
Build "Data types" 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.