Finding a Primitive’s Methods

Mina’s gallery display starts with something small: a room label such as "Gallery A", shown above each temperature reading. In the Python version, that label is a str, and its methods come from the str class. In the JavaScript port, typeof calls the same label a "string", a primitive, yet methods such as .toUpperCase() still work on it. So where do those methods live?

In this lesson, you’ll find out and see how a primitive reaches them.

Look Up Where a String Method Lives

In Python, you’d answer the question by asking the label’s class. Every string is an object, but the methods are stored once, on str, and each instance finds them there:

Language: Python
>>> label = "Gallery A"
>>> type(label) is str
True
>>> "upper" in vars(str)
True

type(label) finds the class, and vars(str) shows only what the class itself stores. In JavaScript, calling label.toUpperCase(), the counterpart of label.upper(), shows that the method is reachable. For the two checks in the Python session, JavaScript has inspection functions.

Remember that a string primitive isn’t an object, so when you call a method on it, JavaScript briefly creates a wrapper object, a String object holding the same text, and then discards it. Called on a string, Object() returns that wrapper so you can look at it. const declares a name you can’t reassign, and console.log() prints a value:

Language: JavaScript
const label = "Gallery A";
const wrapper = Object(label);
console.log(typeof label);
console.log(typeof wrapper);

The label stays a primitive, while the wrapper is an object:

Language: Program Output
string
object

Almost every object has a link to one other object, its prototype. When a property is missing on the object, JavaScript looks it up on the prototype instead. That’s all you need to know about prototypes for now, since they get a course of their own later.

Now you can ask where "toUpperCase" is stored. Object.hasOwn() answers whether an object stores a property itself, String.prototype is the prototype that holds the methods every string shares, and Object.getPrototypeOf() returns a value’s prototype. You don’t need Object() to call these inspection functions. Hand them a string, and they wrap it the same way a method call does. Here, === compares two objects by identity, like Python’s is:

Language: JavaScript
const label = "Gallery A";
console.log(Object.hasOwn(label, "toUpperCase"));
console.log(Object.hasOwn(String.prototype, "toUpperCase"));
console.log(Object.getPrototypeOf(label) === String.prototype);

The string’s wrapper has no toUpperCase of its own, but String.prototype has one, and the wrapper is linked to that prototype:

Language: Program Output
false
true
true

Line these up with the Python checks. The last line plays the same role as type(label) is str, though Object.getPrototypeOf() returns the shared methods object, not a class you call. The middle line corresponds to "upper" in vars(str). The first line has no Python partner, because a str has nowhere to store instance attributes, so vars(label) raises a TypeError.

When you call label.toUpperCase(), JavaScript wraps the primitive in a temporary String object, then looks on the wrapper’s prototype, String.prototype, and finds the method there. The method returns a new string, and JavaScript discards the wrapper.

That’s where the Python analogy stops. In Python, "Gallery A" is itself an instance of str, while a JavaScript primitive has no prototype link of its own, so Object.getPrototypeOf() reports its wrapper’s link. You may hear that everything in JavaScript is an object. It’s more accurate to say that almost everything can be used like an object.

Keep the Label a Plain Value

A primitive is an immutable value, so the label can’t gain a property. The examples in this course run as ECMAScript (ES) modules, from a .mjs file or with "type": "module" in package.json.

Here, try runs a block, catch receives the error if the block throws, and err.name holds the error’s type:

Language: JavaScript
const label = "Gallery A";
try {
  label.room = 2;
} catch (err) {
  console.log(err.name);
}

The module refuses the write:

Language: Program Output
TypeError

Modules always use strict mode, a stricter set of rules that turns some silent mistakes into errors. That’s why the failed write surfaces as a TypeError. Outside strict mode, the write fails silently, and nothing is stored. Python refuses too, with an AttributeError.

Seeing the methods on String.prototype may tempt you to add a gallery helper there. Don’t change built-in prototypes. Every string in the program, including those in code you didn’t write, would share your method, and a future JavaScript version could add one with the same name. Keep helpers as ordinary functions that take the label.

To convert a value, you call a conversion function as you would in Python, writing String(x) where you’d write str(x) and Boolean(x) for bool(x). String() and Boolean() return primitives, but the results follow JavaScript’s own rules, which later lessons cover. JavaScript also has a second spelling that Python lacks. The new keyword calls a function as a constructor, like calling a class in Python, and with String it builds a lasting wrapper:

Language: JavaScript
const label = "Gallery A";
console.log(typeof String(label));
console.log(typeof new String(label));

Only the plain call gives you a primitive back:

Language: Program Output
string
object

That wrapper is a separate object, so it’s a different value from the text it holds. Like every object, it also counts as true in a condition, regardless of its contents.

So keep the label a plain string, let the language create and discard wrappers behind the scenes, and call String() and Boolean() without new when you convert values. When you want to know where a method comes from, look at String.prototype and leave it unchanged.

In the next exercise, Keep the Display Flag False, you’ll fix a separate gallery display config so it supplies the values the display expects for one room.

Become a Member to join the conversation.