Skip to content

strict mode

Strict mode is a restricted variant of JavaScript that turns several silent mistakes into thrown errors, removes a few error-prone features, and reserves words for future syntax.

Strict mode arrived in ES5. A script or function opts in with the "use strict" directive, a string literal placed before any other statement in its body. ES modules and class bodies are always strict, and no directive turns strict mode off. Code that isn’t strict is informally called sloppy mode.

In strict code, assigning to an undeclared variable throws a ReferenceError instead of creating a global property. Writing to a read-only property throws a TypeError. A plain function call gets undefined as its this binding, and the with statement is a syntax error.

Strict mode also reserves nine words that sloppy code can use as variable names, the strict-mode reserved words. Six of them, implements, interface, package, private, protected, and public, have no meaning yet. The other three, let, static, and yield, are keywords.

Example

Say a checkout script adds up a cart, but one line misspells the running total. The Node.js REPL runs in sloppy mode, so the typo fails silently:

Language: JavaScript
> function cartTotal(prices) {
…   let total = 0;
…   for (const price of prices) {
…     totl = total + price;
…   }
…   return total;
… }
undefined

> cartTotal([5, 12.5])
0
> globalThis.totl
12.5

The function returns the wrong total, and the misspelled name leaks out as a new global property. The same function in an ES module runs in strict mode:

Language: JavaScript Filename: cart-total.mjs
function cartTotal(prices) {
  let total = 0;
  for (const price of prices) {
    totl = total + price;
  }
  return total;
}

console.log(cartTotal([5, 12.5]));

Running the module stops at the misspelled assignment instead of printing a wrong total:

Language: Shell
$ node cart-total.mjs
ReferenceError: totl is not defined

The error fires on the first pass through the loop and names the misspelled variable, so no wrong total ever reaches the caller.

A developer operates a bike-share report machine with gauges, bicycle cards and panels for names, arrays, strings and functions.

Course

A First Tour of JavaScript for Pythonistas (Fast Track)

Get a quick first look at JavaScript by porting a small Python report, skipping what matches Python and focusing on what JavaScript does differently with names, strings, decisions, loops, functions, arrays, and objects.

basics js py

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 23, 2026