Skip to content
Tutorials Center
My learning

Topics

Lesson 68 of 156 ยท Errors and Debugging

Strict Mode

Learn what "use strict" does, which silent mistakes become real errors, how this changes, and why modules and classes are always strict.

Early JavaScript was designed to keep going no matter what. Misspell a variable name while assigning to it, and JavaScript would quietly create a brand-new global variable. Write to a read-only property, and it would silently do nothing. That forgiving behavior hides bugs instead of reporting them.

Strict mode is an opt-in, stricter version of the language, added in ES5 (2009). It turns many of those silent mistakes into real errors, removes a few confusing features, and makes this behave more predictably. The good news: if you write modules or classes, you are already using it. This lesson shows what changes, how to turn it on, and where it's switched on for you.

Turning on strict mode

Put the string "use strict"; at the very top of a script or a function. It's an ordinary string expression, so old engines that don't understand it simply ignore it, which is how it was added without breaking the web.

JavaScript
"use strict";
// the whole script is strict from here

function helper() {
  // strict too: functions inherit it
}

Or for a single function:

JavaScript
function careful() {
  "use strict";
  // only this function (and functions inside it) is strict
}

The rules for the directive are precise:

  • It must be the first statement of the script or function body. Comments and blank lines before it are fine; any real code before it turns it into a meaningless string.
  • Single or double quotes both work, but it must be spelled exactly use strict, in lowercase.
  • There's no way to turn strict mode off again inside strict code.
  • A function with default values, rest parameters or destructuring in its parameter list can't contain its own "use strict"; that's a SyntaxError. Make the surrounding script strict instead.

About the editors in this course. The console editors run your code as a classic script, which is not strict unless you write "use strict". That makes them handy for comparing both modes side by side, as the examples below do.

Mistake 1: accidental globals

In sloppy (non-strict) mode, assigning to a name that was never declared creates a global variable. One typo, and your program has two variables where you thought it had one. Strict mode throws a ReferenceError instead:

JS Accidental globals

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The sloppy version returns 0 with no complaint, and leaks totl into the global scope. The strict version points straight at the bug.

Mistake 2: assignments that silently fail

Some assignments can never succeed: writing to a frozen object, to a getter-only property, to a non-writable built-in like undefined, or adding a property to a primitive string. Sloppy mode ignores them; strict mode throws a TypeError.

JS Silent failures become TypeErrors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Silent failures are some of the worst bugs to track down: the code looks like it worked. A thrown error, with a line number, is far kinder. (Working with Objects covers Object.freeze().)

Deleting things that can't be deleted follows the same pattern. delete Object.prototype returns false in sloppy mode and throws a TypeError in strict mode.

this in plain function calls

When you call a function on its own, not as a method, what is this? In sloppy mode it's the global object (window in browsers), so careless code can read and write globals through this. In strict mode it's undefined, which makes mistakes fail fast:

JS this without an object

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In sloppy mode, the detached inc() would have tried to increment window.count, quietly creating a global NaN. Strict mode throws a TypeError the moment you lose this. The this Keyword explores this further.

Syntax that strict mode forbids

Some old features are banned outright in strict code. The engine rejects them while reading the script, with a SyntaxError, before anything runs. This example compiles small snippets with the Function constructor so you can see each one fail without breaking the whole editor:

JS Syntax that strict mode rejects

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What each rule prevents:

  • Duplicate parameter names like function f(a, a): the second silently shadowed the first.
  • Legacy octal literals like 010, which means 8, not 10. Write 0o10 if you really mean octal.
  • The with statement, which made it impossible to know which variable a name refers to.
  • Deleting plain variables, which never worked anyway.
  • Future reserved words such as implements, interface, package, private, protected, public and static can't be used as variable names.

Other changes

A few more differences are worth knowing, mostly so old code doesn't surprise you:

  • eval gets its own scope. Variables declared inside eval("var x = 1") no longer leak into the surrounding function.
  • arguments no longer mirrors parameters. In sloppy mode, changing a parameter also changed arguments[0] and vice versa. In strict mode they are independent. (Modern code uses rest parameters instead of arguments anyway.)
  • arguments.callee is forbidden. Give the function a name and refer to that instead.
  • this is no longer boxed. Calling a function with call(5) gives it this === 5, not a Number object.

Modules and classes are always strict

You rarely need to type "use strict" in modern code, because the places where most new code lives are strict automatically:

  • ES modules: any file loaded with <script type="module"> or import. See Modules.
  • Class bodies: all code inside a class { ... }, including constructors and methods.

This editor runs as a module, so it's strict without the directive. Notice also that this at the top level of a module is undefined:

JS Modules are strict automatically

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Node.js treats files as modules when they end in .mjs or when package.json says "type": "module". Classic CommonJS files in Node.js (require()) are sloppy unless you add the directive.

Watch out when combining old scripts. If you concatenate a strict file and a sloppy file into one, the directive at the top of the first file can make the second one strict too, and break it. Build tools handle this properly today, but it's why older guides recommend putting "use strict" inside a wrapper function rather than at the top of a shared file.

Should you use it?

Yes. Strict mode costs nothing and catches real bugs. In practice:

  • Writing modules or classes? You're already strict. Nothing to do.
  • Writing a classic <script> or a CommonJS file? Put "use strict"; on the first line.
  • Maintaining old code? Turn it on one function at a time and run the tests, because code that relied on sloppy behavior may start throwing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In strict mode, what happens when you assign to a variable that was never declared?

    Sloppy mode creates an accidental global; strict mode reports the mistake with a ReferenceError.
  2. 2. Which code is strict without writing "use strict"?

    Modules and class bodies are always strict. Ordinary functions inherit strictness from their script, which is sloppy by default.
  3. 3. What is this inside a strict function called as doSomething(), not as a method?

    Strict mode leaves this as undefined for plain calls, so lost-this bugs fail fast instead of touching globals.
  4. 4. Why does this directive have no effect? let count = 0; "use strict";

    The directive only works at the very start of a script or function body; anywhere else it's just an unused string.

Summary

  • "use strict"; as the first statement of a script or function enables strict mode for that code.
  • Accidental globals become ReferenceErrors; impossible assignments and deletions become TypeErrors.
  • this is undefined in plain function calls instead of the global object.
  • Confusing syntax such as with, duplicate parameters and legacy octal literals is a SyntaxError.
  • ES modules and class bodies are always strict, so most modern code already is.
  • Use strict mode everywhere; add it carefully to old code and test.

Next: How Asynchronous JavaScript Works โ€” the single thread, the event loop and the task queues behind every timer and promise.