The last lesson ended on a question: What is this in a call with no dot at all, such as showThis()? The answer depends on which set of rules the function itself runs under, and your plant tracker has been choosing one all along.
In this lesson, you’ll find out which mode a file or a REPL session runs in, predict how the same code behaves in each, and fix code that only worked because the older rules let a mistake slide.
Know Which Mode Your Code Runs in
JavaScript keeps two sets of rules so that old code doesn’t break. The original rules, often called sloppy mode, let some mistakes pass in silence. Strict mode came later and turns many of those silent mistakes into errors you can see and fix.
A classic script switches to strict mode with "use strict"; before any other statement. Your tracker doesn’t need it. The package.json in your plant-tracker folder sets "type": "module", so Node.js runs every .js file there as an ES module, and modules are always strict. Every script you’ve run in this course was strict. Classes, which come later in this course, are always strict too.
The Node.js REPL is the exception: it runs your lines in sloppy mode. So the same lines can behave differently there and in a file.
Compare the REPL With a Module
Say you write a small water() function for Goldie and mistype the variable’s name inside it. Start node and try it in the REPL first:
> let daysSinceWatered = 6;
undefined
> function water() {
… daysSinceWaterd = 0;
… }
undefined
> water()
undefined
> daysSinceWatered
6
> globalThis.daysSinceWaterd
0
Nothing complained, yet Goldie was never watered: daysSinceWatered is still 6. In sloppy mode, assigning to a name that was never declared quietly creates a property on globalThis, the global object that holds JavaScript’s and Node.js’s built-in names. That’s where the 0 went.
The global object also shows up in calls without a dot. Type this function into the same session:
> function showThis() {
… return this === globalThis;
… }
undefined
> showThis()
true
In sloppy mode, a plain call’s this falls back to the global object. Type .exit to leave the REPL.
Now run the same ideas as a module. Save this as strict-check.js in your plant-tracker folder:
strict-check.js
function showThis() {
return this;
}
console.log("this in a plain call:", showThis());
let daysSinceWatered = 6;
function water() {
daysSinceWaterd = 0;
}
water();
console.log("Days since watered:", daysSinceWatered);
Before you run it with node strict-check.js, predict what each console.log() line prints, if it prints at all.
$ node strict-check.js
this in a plain call: undefined
file:///.../plant-tracker/strict-check.js:8
daysSinceWaterd = 0;
^
ReferenceError: daysSinceWaterd is not defined
In strict mode, a plain call’s this is undefined. The typo no longer creates a global either. It throws a ReferenceError that names the misspelled variable and the line it’s on, so the second console.log() never runs.
The same typo was in your REPL session, where sloppy mode hid it.
If the two plain-call results still seem puzzling, select the button below. It opens a conversation with your mentor, who can walk you through why a plain call’s this was the global object in the REPL and undefined in the module:
Fix Code That Leans on Sloppy Mode
Code written for sloppy mode often breaks once it runs as a module. Don’t fix it by switching strict mode off. Change the code so that it does what you meant.
For an undeclared name, that means one of two things. If it’s a typo, correct it. If you really need a new variable, declare it with const or let. Change line 8 of strict-check.js to use the right name:
strict-check.js
function showThis() {
return this;
}
console.log("this in a plain call:", showThis());
let daysSinceWatered = 6;
function water() {
daysSinceWatered = 0;
}
water();
console.log("Days since watered:", daysSinceWatered);
Run it again:
$ node strict-check.js
this in a plain call: undefined
Days since watered: 0
Goldie is watered. The rest of the course relies on the first line of output: when a strict function that uses this is called without a dot, it gets undefined, not the global object.
What counts is the mode of the function being called, not the mode of the code that calls it. Every ordinary function you define in a module is strict, so it gets undefined in a plain call wherever that call happens.
A function can also opt in on its own: with "use strict"; before any other statement in its body, it’s strict even in the sloppy REPL. That’s all you need to know about plain calls for now. MDN’s strict mode reference lists everything else strict mode changes.
Check Your Understanding
Use these questions to check that you can tell the two modes apart:
- Ravi types
plantCount = 4into the Node.js REPL, with noletorconst. What happens? What happens if the same line is in a.jsfile in theplant-trackerfolder? - In a module, what does
showThis()return whenshowThis()is a plain function that returnsthis? - A script in the
plant-trackerfolder has the linetotal = 0;and no other mention oftotal. How do you fix it so it runs?
- The REPL runs in sloppy mode, so the line quietly creates
plantCounton the global object. In theplant-trackerfolder, the file is a module, so it’s strict and the line throwsReferenceError: plantCount is not defined. undefined.showThis()is defined in a module, so it’s strict and getsundefinedforthiswhen called without a dot.- Declare the variable with
let, as inlet total = 0;. In sloppy mode, the line would quietly create a global, but the module is strict, so assigning to an undeclared name throws aReferenceError.
Your modules have been strict all along, while the REPL runs in sloppy mode. A strict function called without a dot gets undefined for this.
You’ll put methods to work next, in the course’s first on-site coding exercise. There, you write code right in your browser and select Run Tests—or press Ctrl+Enter, or Cmd+Enter on a Mac—to have automated tests check it.
Run the tests early and often. When a test fails, read the feedback and any error message for clues, then revise your code and run the tests again. The Hints tab helps if you get stuck, and selecting Full screen or pressing T gives you more room.
Head to Exercise: Turn Plant Functions Into Methods, where you turn Mei’s standalone plant care functions into methods.