Reading the Spec’s Conversion Steps

Mina’s Reading class now converts on purpose: valueOf() gives the number, toString() gives the label, and ToPrimitive picks between them by hint. The next job is messier. The gallery’s export has rows from sensors that went offline, so some readings arrive as null or undefined, and the curators’ notes come as arrays.

Before Mina writes any cleanup code, she wants to know what Number(), String(), and Boolean() will do with those values. Memorizing a list of odd results won’t scale. The ECMAScript specification, the document that defines JavaScript, writes each conversion as a short numbered list of steps. Once you can follow those steps, you can work out a result and then check it.

Separate the Spec’s Names From Your Functions

In Python, float(x) follows a fixed procedure. It parses a string itself, and for other objects it calls the type’s __float__, falling back to __index__. JavaScript’s conversion functions also run a fixed list of steps, which the specification calls an abstract operation. The difference is where an object gets a say. Python hands an object’s conversion to its special methods, while JavaScript consults an object only at a ToPrimitive step.

Besides ToPrimitive, which you’ve met, conversions use ToNumber, ToString, and ToBoolean. Here’s the core of ToNumber, quoted from the current specification draft. The ... lines mark steps left out for true, strings, and internal checks:

1. If arg is a Number, return arg.

2. If arg is either a Symbol or a BigInt, throw a TypeError exception.

3. If arg is undefined, return NaN.

4. If arg is either null or false, return +0𝔽.

…

8. Let primitiveValue be ? ToPrimitive(arg, number).

…

10. Return ? ToNumber(primitiveValue).

A ? passes any error from that step straight through, and 𝔽 marks a JavaScript Number. Steps three and four answer Mina’s question about missing readings before any object gets involved. In step eight, an object goes through ToPrimitive with the number hint, and the result starts ToNumber again.

These names exist only in the specification. JavaScript has no global called ToNumber, as you can see in Node’s interactive prompt by running node with no filename:

Language: Node.js
> typeof ToNumber
'undefined'
> ToNumber(null)
Uncaught ReferenceError: ToNumber is not defined

typeof answers 'undefined' for a missing name, and using that name throws a ReferenceError, JavaScript’s error for an unknown name. The functions you call are Number(), String(), and Boolean(). Each runs its abstract operation for you, sometimes after a check of its own. Number() converts a BigInt itself rather than letting step two throw, rounding it to the nearest Number when it’s too large to store exactly.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.