LearnThatStack Ace your next interview
JavaScript · question
Question 3 of 126

Explain the concept of "use strict" in JavaScript

beginner
← All JavaScript questions
Re-explain

"use strict" turns on strict mode, which makes JavaScript throw errors for mistakes it would otherwise ignore and blocks a few unsafe actions. You put it at the top of a script or a function.

In strict mode:

  • Assigning to an undeclared variable throws instead of creating a global.
  • Duplicate parameter names are a syntax error.
  • this is undefined in a plain function call, not the global object.
  • Deleting a variable, a function or arguments is not allowed.

These rules make code safer and easier for engines to optimize. Modules and class bodies are strict automatically, with no directive needed.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • In strict mode, what happens when a plain function that sets this.name is called without new? this is undefined, so the assignment throws a TypeError. In sloppy mode it quietly writes a property on the global object instead.
  • Where exactly must the directive go, and what happens if it isn't there? It must be the first statement of the script or function body. Anywhere else it's a plain string expression and does nothing.
  • What goes wrong when you add strict mode to legacy code or concatenate strict and non-strict files? Code that relies on implicit globals breaks. After concatenation the directive covers the whole bundle, or none of it if not first. Scope it to functions.
  • How does strict mode treat writes that sloppy mode silently ignores, like writing to a frozen object? Writes to non-writable or getter-only properties, or adding to a non-extensible object, throw a TypeError instead of failing silently.
  • Why is the with statement not allowed in strict mode? with makes name lookup depend on an object's runtime contents, so neither readers nor engines can tell which variable a name refers to.

What you can say

  1. 'use strict' turns on strict mode, which makes JavaScript throw errors for mistakes it would otherwise let slide and blocks a few unsafe actions.
  2. You enable it by writing the string at the top of a script or a function, and modules and class bodies are already strict, so you skip it there.
  3. The change I'd lead with is undeclared variables. Assigning to one throws instead of quietly creating a global, so a typo fails right away.
  4. Also, this is undefined in a plain function call rather than the global object, duplicate parameter names are a syntax error, and deleting a variable, function or arguments isn't allowed.
  5. Those rules make code safer and easier for engines to optimize.
  6. The catch is old code. Scripts that lean on implicit globals or on this being the global object can break when you turn it on, so for new code I just use modules.

Weak answers to avoid

  • Says it makes code cleaner, with no specifics Too vague to score. Name concrete rule changes, like assigning to an undeclared variable throwing and this being undefined in a plain call.
  • Thinks modules and classes need the directive ES modules and class bodies are already strict. Adding it there does nothing, and saying it's required suggests you haven't checked how modern code runs.
  • Claims this becomes undefined everywhere Only in a plain function call. Method calls, bound functions and arrow functions keep their usual this. Say plain call, not everywhere.
  • Says the directive can go anywhere in the file It only counts as the first statement of a script or function. Placed later it's an ignored string and the code runs in sloppy mode.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime