Strict Mode

Easy

Strict mode is an opt-in restricted variant of JavaScript that eliminates silent errors, prevents unsafe features, and paves the way for future language improvements. Activated by "use strict" at the top of a script or function, it changes several default behaviors.

Interactive Visualization

Object Patterns

Old Way
const user = { first: "Alice", last: "Smith" };
const full = user.first + " " + user.last;
// Must compute manually every time
Modern Way
Click transform to see

Key Points

  • "use strict" at the top of a script or function body enables strict mode
  • ES modules and class bodies are always in strict mode — no directive needed
  • Standalone function this is undefined instead of the global object
  • Assigning to undeclared variables throws ReferenceError instead of creating globals
  • Duplicate function parameters and octal literals are syntax errors
  • Assigning to read-only properties throws TypeError

Code Examples

Enabling Strict Mode

'use strict'

x = 10 // ReferenceError: x is not defined

// ES modules are ALWAYS strict
// Classes are ALWAYS strict
class MyClass {
  method() {
    // strict mode automatically
  }
}

Strict mode applies per-script or per-function. Modules and classes are implicitly strict.

this Behavior Changes

function sloppyThis() { return this }
// sloppyThis() → window (browser) or global (Node)

function strictThis() {
  'use strict'
  return this
}
console.log(strictThis()) // undefined

// Catches accidental constructor calls
'use strict'
function User(name) {
  this.name = name // TypeError if called without new!
}

In strict mode, standalone function calls have this as undefined. This catches constructor mistakes.

Preventing Silent Errors

'use strict'

const obj = {}
Object.defineProperty(obj, 'readOnly', { value: 42, writable: false })
// obj.readOnly = 99 // TypeError!

const frozen = Object.freeze({ a: 1 })
// frozen.b = 2 // TypeError!

// In sloppy mode, ALL of these silently fail

Strict mode turns silent failures into thrown errors, making bugs visible immediately.

Syntax Restrictions

'use strict'

// No duplicate parameter names
// function sum(a, a) {} // SyntaxError!

// No octal literals
// const octal = 010  // SyntaxError!
const octal = 0o10    // ✅ Use 0o prefix (ES6)

// eval has its own scope
eval('var evalVar = 42')
// console.log(evalVar) // ReferenceError!

Strict mode forbids confusing syntax: duplicate params, old-style octals, eval variable leakage.

Common Mistakes

  • Placing "use strict" after other statements — must be the first expression
  • Not realizing ES modules are already strict
  • Expecting strict mode to propagate into called functions
  • Forgetting that class bodies are always strict

Interview Tips

  • Key this difference: undefined vs globalThis in standalone functions
  • Modules and classes are implicitly strict
  • List 3+ things strict mode catches: accidental globals, read-only writes, duplicate params
  • Strict mode enables future optimizations by engines

Related Concepts