EasyCore JS📖 Theory Question

What does "use strict" do and why should you use it?

💡

Hint

Opt-in to stricter parsing — catches silent errors, changes some behaviors

Full Answer

Strict mode activates a restricted variant of JavaScript that converts silent errors into thrown errors and disables some confusing/dangerous features.

What it prevents:

  • Implicit globals — x = 5 throws ReferenceError (not window.x)
  • Duplicate parameter names — function fn(a, a) {} throws
  • Writing to read-only properties — throws instead of silently failing
  • this in standalone functions is undefined (not window)
  • delete on variables/functions — throws
  • Octal literals (0777) — throws
'use strict';

x = 5;             // ReferenceError — no more accidental globals
function fn(a, a) {} // SyntaxError

function test() {
  console.log(this); // undefined (not window)
}
test();

Good news: ES6 modules and classes are always in strict mode automatically. In modern code you rarely need to write 'use strict' explicitly.

💡 Enable strict mode per file or per function with the string 'use strict' at the top. Helps catch bugs early and enables engine optimizations.

More Core JS Questions

EasyWhat is the difference between var, let, and const?EasyExplain closures with a practical example.EasyWhat is hoisting in JavaScript?EasyExplain the event loop, call stack, and microtask queue.

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint