Strict Mode

Strict mode changes several historically permissive JavaScript behaviors. It can be enabled with "use strict", although ECMAScript modules are automatically strict. Strict mode prevents accidental global variable creation, changes some this behavior, rejects certain duplicate or restricted constructs, and makes several silent failures become errors.

Strict mode is not a security sandbox. Its purpose is to make language semantics safer and less error-prone.

Strict Mode Prevents Accidental Globals
"use strict";

function createUser() {
  username = 'Suriya';
}

createUser();
💡 Without strict mode, assigning to an undeclared identifier in older non-module code could create or attempt to create a global property. Strict mode throws a ReferenceError instead.
Key Takeaways
  • ES modules execute in strict mode automatically.
  • Strict mode catches several classes of accidental programming errors.
  • Strict mode is not equivalent to a security boundary.
Knowledge Check

What happens when an undeclared variable is assigned inside strict-mode code?