SKU // 59

JavaScript Strict Mode

Early JavaScript was designed to be forgiving: many mistakes were silently ignored instead of reported. Strict mode is an opt-in setting that turns those silent mistakes into real errors, so you find bugs right away instead of hours later.

// Turning It On

Put the string "use strict"; as the very first statement of a script or a function:

"use strict";       // whole script is strict

let x = 5;
// ...
function careful() {
  "use strict";     // only this function is strict
  // ...
}
Watch out
It must be the first statement. Anywhere else it is just an ordinary string that does nothing, with no warning.

// A Directive, Not a Statement

"use strict" arrived in ECMAScript 5 (2009). It was designed so that older browsers would not break: a lone string like "use strict"; is a perfectly legal expression that does nothing, just like writing "John Doe"; or 4 + 5; on a line by itself. Old browsers simply ignored it. Newer JavaScript engines recognize that exact string at the top of a script or function as a directive: an instruction about how to run the code.

Where you put it decides how much code is strict:

x = 3.14;            // fine: this part is not strict
myFunction();

function myFunction() {
  "use strict";
  y = 3.14;          // ReferenceError: only this function is strict
}
ReferenceError: y is not defined

// Already On Automatically

Code inside a class body and code in JavaScript modules (<script type="module">) is always strict. Most modern code is strict even when you never typed the words.

// Change 1: Undeclared Variables Throw

The single most useful change. Without strict mode, assigning to a misspelled name quietly creates a new global variable:

// sloppy mode
let total = 0;
totl = 50;               // typo: creates a global "totl", no error
console.log(total);      // 0  ...why didn't it update?
0
"use strict";
let total = 0;
totl = 50;               // ReferenceError: totl is not defined
ReferenceError: totl is not defined

Strict mode points straight at the typo.

// Change 2: this Is undefined in Plain Function Calls

"use strict";

function whoAmI() {
  return this;
}

console.log(whoAmI());   // undefined  (sloppy mode: the whole window object)
undefined

Remember "losing this" from the Object this lesson? In strict mode, a lost this is undefined, so reading this.name throws a clear error instead of quietly reading something off window.

// Change 3: Failed Assignments Throw

Some values cannot be changed. Sloppy mode ignores attempts to change them; strict mode reports them:

"use strict";

const settings = Object.freeze({ theme: "dark" });
settings.theme = "light";
// TypeError: Cannot assign to read only property 'theme' of object '#<Object>'

NaN = 5;
// TypeError: Cannot assign to read only property 'NaN' of object '#<Window>'
TypeError: Cannot assign to read only property 'theme' of object '#<Object>' TypeError: Cannot assign to read only property 'NaN' of object '#<Window>'

// Change 4: Duplicate Parameter Names Are Banned

"use strict";

function add(a, a) {   // SyntaxError: Duplicate parameter name not allowed
  return a + a;
}

// Change 5: Future Keywords Are Reserved

Words JavaScript might use later cannot be used as variable names:

"use strict";
let interface = 1;   // SyntaxError: Unexpected strict mode reserved word
// also reserved: implements, package, private, protected, public, static, yield

// Other Removals

Banned in strict mode Why
with (obj) { ... }Made it impossible to tell which variable a name meant
let n = 010;Leading-zero octal numbers were confusing (010 was 8). Use 0o10
delete myVariable;Deleting a plain variable never worked; now it is a syntax error
let x = "\010";Octal escape characters in strings, for the same reason
delete myFunction;Deleting a function name is banned, like deleting a variable
let eval = 1;
let arguments = 1;
These names are reserved for JavaScript's own use
obj.x = 1; (x has only a getter)Writing to a read-only or get-only property now throws instead of failing silently
eval("var z = 2");eval can no longer create variables in the surrounding code

// Why Use It

  1. Catches typos: misspelled variable names fail loudly
  2. Safer this: a lost this cannot silently touch the global object
  3. No silent failures: writes that do nothing now tell you so
  4. Future-proof: reserved words stay free for new language features
Rule of Thumb
Add "use strict"; to the top of every standalone script you write. It costs nothing and every error it throws is a bug you would have had to hunt for later.

// Quick Check

Where must "use strict"; go to take effect?

  1. Anywhere in the script
  2. At the very end of the script
  3. As the first statement of a script or a function
  4. Inside a comment
Show answer
C. Anywhere else it is just an ordinary string that does nothing, with no warning.

What does this function return?

function who() {
  "use strict";
  return this;
}
who();
Show answer
undefined. In strict mode, a plain function call (no object and dot in front) gets undefined as this, instead of the global window object.

Which of these is not changed by strict mode?

  1. Assigning to an undeclared variable
  2. Using interface as a variable name
  3. Adding two numbers with +
  4. Giving a function two parameters with the same name
Show answer
C. Normal code works exactly the same. Strict mode only turns sloppy mistakes (A, B, D) into errors.

// Try It

  1. Click Open Playground and run it: the typo quietly creates a global. Then add "use strict"; as the first line and run it again
  2. Wrap the same code in a function whose first line is "use strict";, call it, and read the error
  3. Freeze an object and try to change a property, with and without strict mode
  4. Put "use strict"; on the second line of a function instead of the first, and confirm it has no effect
// Video Walkthroughs
Last updated: Oct 10, 2026, 12:53 AM ET