Hiprup

What is strict mode in JavaScript?

Strict mode is an opt-in stricter version of JavaScript, enabled with "use strict" at the top of a file or function. ES modules and classes turn it on automatically.

  • Catches silent errors — assigning to an undeclared variable now throws.

  • Prevents bad practices — no duplicate parameter names, no accidental globals.

  • Safer this — this is undefined in plain function calls instead of the global object.

Why use it: it turns sloppy, silently-ignored mistakes into clear errors, making code safer and easier to debug.

'use strict';

// Prevents accidental globals
// x = 10; // ReferenceError: x is not defined

// this is undefined in functions (not window)
function showThis() {
  console.log(this); // undefined (not window)
}

// Cannot delete variables
let a = 10;
// delete a; // SyntaxError

// No duplicate parameters
// function sum(x, x) {} // SyntaxError

// Cannot assign to read-only
const obj = {};
Object.defineProperty(obj, 'name', { value: 'John', writable: false });
// obj.name = 'Jane'; // TypeError

// Octal literals not allowed
// const n = 010; // SyntaxError (use 0o10 instead)

Without strict mode, x = 10 silently creates a global variable. With strict mode, it throws ReferenceError. this in standalone functions is undefined (not window).

Duplicate parameters, deleting variables, and old octal syntax all throw errors. These prevent common bugs.

Know 4-5 specific behaviors: no accidental globals, undefined this in functions, no duplicate params, no deleting variables. Mention that ES modules and classes are automatically strict. 'use strict' is mostly unnecessary in modern module-based code.

What is strict mode in JavaScript? | Hiprup