JavaScript's Type System Under the Hood (Summary)

Congratulations, you made it to the end of the course! Mina’s gallery display now runs in JavaScript. Its Reading class keeps the Celsius value apart from the printed label, and the formatter prints Reading: 21 C; next: 22 C, the same line as the Python version.

Getting there meant finding out what JavaScript does when a value isn’t of the type an operation needs. Formatting a reading in a template literal, passing it to Number(), and adding one to it each start a conversion, and they don’t all ask for the same thing.

In this course, you learned how to:

  • Find where a primitive’s methods live, on a prototype such as String.prototype, and see how a temporary wrapper object reaches them
  • Follow ordinary conversion, where a hint decides whether toString() or valueOf() runs first
  • Trace the specification’s ToNumber, ToString, and ToBoolean steps to find what Number(), String(), and Boolean() return, including the BigInt and symbol checks that Number() and String() run first
  • Give a class one conversion hook under Symbol.toPrimitive that answers the "string", "number", and "default" hints on purpose
  • Convert at the boundary with String(), Number(), or a template literal, rather than letting an operator choose for you

Let Wrappers Work Behind the Scenes

In Python, a string is an instance of str, and its methods are stored on that class. A JavaScript string is a primitive with no properties of its own. When you call label.toUpperCase(), JavaScript wraps the label in a temporary String object, finds the method on the wrapper’s prototype, String.prototype, and then discards the wrapper.

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.