Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Strict Mode

Strict mode is enabled with the "use strict" directive at the top of a script or function. It changes JavaScript to a stricter execution mode; classes and modules enable it automatically.

How it works

Strict mode is enabled by placing the exact directive "use strict" at the beginning of a script or function. A script-level directive affects the whole script, while a function-level directive affects only that function. Classes and modules enable strict mode automatically.

Syntax

"use strict";

Key points

  • Use the exact string directive to opt a script into strict mode.
  • At script level, the directive applies to the entire script.
  • Placing the directive at the beginning of a function limits strict mode to that function.
  • Classes and modules activate strict mode automatically, so an explicit directive is unnecessary there.
  • Strict mode is one-way for the current execution context.

Common mistakes

  • The directive must appear at the top of the script; code before it can prevent strict mode from being enabled.
  • Strict mode cannot be turned off once execution has entered it.

Example: Strict mode rejects accidental globals

'use strict';

function assignUndeclared() {
  undeclared = 1;
}

try {
  assignUndeclared();
} catch (error) {
  console.log(error instanceof ReferenceError);
}

The strict function throws instead of silently creating a global variable.

When to use it

Script-wide validation

Enable stricter checks in a traditional script.

Function-scoped strict mode

Constrain legacy code to a stricter function body.

Automatic strict mode

Use classes or modules without adding a directive.

  • classes
  • modules
  • scripts
  • functions

Source: The modern mode, "use strict". Checked 2026-10-15.