The readings check now keeps names like "ridge #1412" in seen instead of whole reading objects and skips the resent ridge reading. Two reading objects with the same contents never matched, yet "rid" + "ge" === "ridge" is true. Why do strings get compared by their contents when objects don’t?
Strings, numbers, and Booleans can’t change, in JavaScript as in Python. In this lesson, you’ll see what that means for the port and how a value that isn’t an object can still have methods.
Make a New String Instead of Changing One
The website shows each station’s name in capitals above its readings. In Python, .upper() gives you a new string and leaves the old one alone:
>>> station = "ridge"
>>> station.upper()
'RIDGE'
>>> station
'ridge'
The JavaScript port spells the method .toUpperCase():
const station = "ridge";
console.log(station.toUpperCase());
console.log(station);
As in Python, you get a new string, and station still holds the original:
RIDGE
ridge
Strings are one of JavaScript’s primitives, along with numbers, Booleans, null, undefined, and the rarer bigint and symbol types. Every primitive is an immutable value. Like values of Python’s str, int, and bool types, it can never change in place, so a method that transforms a string, such as .toUpperCase(), returns a new string instead.
Immutability is what makes comparing by value safe. Nobody can change "ridge", so it doesn’t matter which copy you hold, and === compares strings and numbers by their values, with two number exceptions you’ll meet later. Here the analogy with Python stops. A Python string is still an object with an identity that is can check, while a JavaScript string or number has no identity at all, only its value.
Watch out: station[0] = "R" throws a TypeError in a strict-mode module, as in Python, but the REPL isn’t in strict mode and only echoes 'R', changing nothing, so try writes like this in a module such as readings-check.js.
Immutability also completes what you know about const. With a primitive, const fixes the value as well as the name, because the value itself can’t change. With an object, const fixes only the name, so you can still change the reading’s fields.
Borrow Methods From a Wrapper Object
If "ridge" isn’t an object, how can it have .toUpperCase() at all? In Python, every value is an object, so the question never comes up. Ask station for its type and its length, and predict what typeof says before you read on:
const station = "ridge";
console.log(typeof station);
console.log(station.length);
The type is a plain "string", yet .length works:
string
5
When you use a property or method on a primitive, JavaScript wraps the value in a wrapper object for that moment, such as a String object for a string, a Number for a number, or a Boolean for a Boolean. The wrapper supplies .length or .toUpperCase(), and then JavaScript throws it away. Only null and undefined have no wrapper, so accessing a property on either raises a TypeError.
Watch out: a property you add to a string, as in station.checked = true, has nowhere to stay once the wrapper is gone, so a module refuses it with a TypeError and the REPL silently loses it.
The same names do a second job as conversion functions. String(x) works like str(x) but by its own rules, so String(true) is "true", not "True". You’ve already met Number(x). It covers whole and fractional numbers, which share one type in JavaScript. Here String() and Number() turn ridge’s sequence number into text and back:
const seqText = String(1412);
console.log(typeof seqText, seqText === "1412");
console.log(Number(seqText) === 1412);
Both return plain primitives, so the comparisons hold:
string true
true
Watch out: new String("ridge") keeps a wrapper object for good, because new builds an object from a constructor the way calling a class does in Python, so typeof calls it "object" and it’s never === to "ridge". Watch out too: new Boolean(false) is an object, and while falsy values include false, 0, "", and null, every object is truthy, so an if statement runs its block for it.
In JavaScript, you treat primitives as plain values. Instead of changing one, you make a new one and store it. Methods and .length come from wrappers that JavaScript creates and discards for you, so you never write new String(), new Number(), or new Boolean() yourself. To convert, call String(x) or Number(x), which return primitives.
In the next exercise, Report the Stations That Moved, you’ll take each station’s location on file and its location in today’s readings. You’ll print a line for every station whose location has changed, giving its new coordinates, and nothing for the stations that are still where they were.