"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.
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.
'use strict' turns on strict mode, which makes JavaScript throw errors for mistakes it would otherwise let slide and blocks a few unsafe actions.
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.
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.
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.
Those rules make code safer and easier for engines to optimize.
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 specificsToo 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 directiveES 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 everywhereOnly 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 fileIt 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.