Back to JavaScript Notes
Topic #316

Strict Mode

You will be able to enable JavaScript strict mode in a script or function and explain how it catches common mistakes such as implicit global variables.

What it is

Strict mode is a way to opt into a stricter, safer variant of JavaScript. You enable it by placing the directive "use strict" at the beginning of a script or function body. It is not a function call; it is a string literal that JavaScript recognizes as a directive.

The mental model is: “run this code with fewer silent mistakes.” In normal, non-strict JavaScript, some errors are ignored or quietly converted into odd behavior. In strict mode, many of those cases throw errors instead.

Related terms include implicit global, directive prologue, function-level strict mode, and script-level strict mode. Modern ES modules and class bodies are already strict by default.

Why it matters

  • It prevents accidental global variables, such as assigning to a misspelled variable name.
  • It makes invalid code fail loudly instead of behaving mysteriously.
  • It helps catch duplicate function parameters and some unsafe uses of delete.
  • It changes the value of this in plain function calls to undefined, reducing accidental global access.
  • It encourages cleaner code by disabling some confusing legacy behaviors.

Syntax or steps

For an entire script, place the directive as the first statement:

"use strict";

For a single function, place it as the first statement inside the function body:

function example() {
  "use strict";
  // strict-mode code here
}

The directive must appear before executable code. Comments are allowed before it, but normal statements are not.

Example

"use strict";

function createGreeter(name) {
  if (!name) {
    name = "world";
  }
  return "Hello, " + name;
}

console.log(createGreeter()); // Hello, world

function leak() {
  secret = 123; // ReferenceError in strict mode
}

try {
  leak();
} catch (error) {
  console.log(error.name); // ReferenceError
}

Part by part:

  • "use strict"; enables strict mode for the whole script.
  • createGreeter() works normally and returns a greeting.
  • secret = 123; tries to assign to an undeclared variable.
  • In non-strict mode, that would create a global variable called secret.
  • In strict mode, it throws a ReferenceError.
  • The try block catches the error so the example can print the error name.

Common mistakes

  • Placing "use strict" after code. Fix: put it as the first statement in the script or function.
  • Thinking function-level strict mode affects the whole file. Fix: use script-level strict mode or modules if you want the entire file strict.
  • Assuming strict mode prevents all bugs. Fix: it catches certain mistakes, but you still need validation, tests, and careful design.
  • Adding it to ES modules expecting a change. Fix: modules are already strict, so the directive is usually unnecessary.

When to use it

Use strict mode when writing plain JavaScript scripts where you want safer behavior. Compare it with non-strict mode and modules:

Context Recommended approach Why
New plain script Add "use strict" at the top Catches implicit globals and other mistakes early
ES module No directive needed Modules are strict by default
Legacy script Add carefully and test Strict mode may reveal existing bugs
Small function Use function-level strict mode Applies stricter rules only where needed

Practice

Guided exercise: Write a function that accidentally assigns to an undeclared variable. Add "use strict" at the top of the script, call the function inside a try block, and print the error name.

Expected output: ReferenceError.

Challenge: Create a function-level strict function called sum() that returns the sum of two numbers and does not create any global variables.

Solution hint: place "use strict"; as the first line inside the function body.

Quick check

Question: Where must "use strict" appear to enable strict mode for a function?

Answer: It must be the first statement in the function body, before any executable code.

Summary

Strict mode is a simple opt-in that makes JavaScript reject several unsafe or confusing behaviors. The key idea is to fail early instead of allowing silent mistakes like accidental globals.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Strict Mode – FAQs

Quick answers about learning Strict Mode in JavaScript.

This free note from Coding Now Tech Institute explains Strict Mode in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Strict Mode, is 100% free with no signup required.
With focused practice, most students grasp Strict Mode in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now