Why Strings and Numbers Can't Change

The readings check now keeps names like "ridge #1412" in seen, and "rid" + "ge" === "ridge" is true, though two reading objects with equal fields never match. Why do strings compare by value when objects don’t? And if a JavaScript string isn’t an object at all, where do its methods come from? The answers explain why changing a station name always means making a new one and why the check never needs new String().

Treat Primitives as Plain Values

Strings, numbers, Booleans, null, undefined, bigints, and symbols are JavaScript’s primitives. Like Python’s str, int, and bool values, they can’t change in place, so a method such as .toUpperCase() returns an uppercase copy, as Python’s .upper() does.

Where Python’s strings are still objects that is can tell apart, a JavaScript string or number has no identity, only its value. That’s why === compares them by value, with two numeric exceptions you’ll meet later. It’s also why a const string can never change, while a const object’s fields can, because const only stops the name from pointing elsewhere.

Borrow Methods From a Wrapper Object

typeof calls station a plain string, yet you can still uppercase it for a report heading and read its length:

Language: JavaScript
const station = "ridge";
console.log(typeof station, station.toUpperCase(), station.length);
console.log(station);

The method hands back a new string, and station is unchanged:

Language: Program Output
string RIDGE 5
ridge

When you access a property or method, JavaScript wraps the primitive in a wrapper object, such as a String or a Number, for that moment only, and throws the wrapper away afterward. null and undefined have no wrapper, so accessing a property on either throws a TypeError.

Four things to watch:

  • station[0] = "R" throws a TypeError in a module, as in Python, but the REPL isn’t in strict mode and only echoes 'R'. Try writes in a module.
  • station.checked = true can’t add a property, because a primitive can’t hold properties of its own. In a module, the assignment throws a TypeError, while Python raises an AttributeError. The REPL silently ignores the assignment. Keep extra data in an object.
  • new String("ridge") keeps a wrapper for good: typeof says "object", and it’s never === to "ridge". Python’s str(x) hands back a plain str. JavaScript’s counterpart is String(x) without new, so String(1412) is "1412".
  • new Boolean(false) is an object, and every object is truthy, so an if runs its block for it. Boolean(false) without new gives a real false.

The JavaScript way is to make a new primitive instead of changing one, to leave wrappers to JavaScript, and to convert with String(x), Number(x), and Boolean(x), never with new. They follow JavaScript’s rules, so String(true) is "true" where Python’s str(True) is "True".

In the next exercise, Report the Stations That Moved, you’ll compare each station’s location on file with its location in today’s readings. You’ll print a line with the new coordinates for every station that has moved and nothing for the rest.

Become a Member to join the conversation.