Lesson guide
Learning objectives
- Explain the main purpose of The modern mode, "use strict" in JavaScript.
- Identify the syntax, APIs, or concepts introduced in this lesson.
- Use the examples to predict how JavaScript will behave before you run similar code.
- Connect this topic to nearby lessons in JavaScript Fundamentals.
Real-world context
This lesson matters when you need to recognize where The modern mode, "use strict" fits into real JavaScript programs. For a long time, JavaScript evolved without compatibility issues.
Key ideas
- The modern mode, "use strict" is part of the JavaScript Fundamentals chapter, so it builds on the surrounding concepts rather than standing alone.
- Read each code example in two passes: first for the result, then for the rule that explains the result.
- When a section compares similar features, focus on the condition that makes you choose one feature over another.
Key terms
- The modern mode, "use strict"
- modern
- use
- strict
- JavaScript Fundamentals
For a long time, JavaScript evolved without compatibility issues. New features were added to the language while old functionality didn’t change.
That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript’s creators got stuck in the language forever.
This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: "use strict".
“use strict”
The directive looks like a string: "use strict" or 'use strict'. When it is located at the top of a script, the whole script works the “modern” way.
For example:
"use strict"; try { undeclaredName = "John"; } catch (error) { console.log(error.name); }
Quite soon we’re going to learn functions (a way to group commands), so let’s note in advance that "use strict" can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script.
Ensure that “use strict” is at the top
Please make sure that "use strict" is at the top of your scripts, otherwise strict mode may not be enabled.
Strict mode isn’t enabled here:
alert("some code"); // "use strict" below is ignored--it must be at the top "use strict"; // strict mode is not activated
Only comments may appear above "use strict".
There’s no way to cancel use strict
There is no directive like "no use strict" that reverts the engine to old behavior.
Once we enter strict mode, there’s no going back.
Browser console
When you use a developer console to run code, please note that it doesn’t use strict by default.
Sometimes, when use strict makes a difference, you’ll get incorrect results.
So, how to actually use strict in the console?
First, you can try to press Shift+Enter to input multiple lines, and put use strict on top, like this:
'use strict'; <Shift+Enter for a newline> // ...your code <Enter to run>
It works in most browsers, namely Firefox and Chrome.
If it doesn’t, e.g. in an old browser, there’s an ugly, but reliable way to ensure use strict. Put it inside this kind of wrapper:
(function() { 'use strict'; // ...your code here... })()
Should we “use strict”?
The question may sound obvious, but it’s not so.
One could recommend to start scripts with "use strict"… But you know what’s cool?
Modern JavaScript supports “classes” and “modules” – advanced language structures (we’ll surely get to them), that enable use strict automatically. So we don’t need to add the "use strict" directive, if we use them.
So, for now "use strict"; is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.
As of now, we’ve got to know about use strict in general.
In the next chapters, as we learn language features, we’ll see the differences between the strict and old modes. Luckily, there aren’t many and they actually make our lives better.
All examples in this tutorial assume strict mode unless (very rarely) specified otherwise.
Common mistakes
- Skipping the small examples and then missing the exact rule that The modern mode, "use strict" depends on.
- Copying code without changing one value at a time to see which part controls the result.
- Treating similar-looking syntax or APIs as interchangeable before checking their edge cases.
Summary
- The modern mode, "use strict" gives you one more piece of the JavaScript mental model.
- The examples in this lesson are the quickest way to check whether the rule is clear.
- Revisit the key terms when you meet the same idea in later chapters.
Predict
Before running this check, predict the two output lines. The important detail is that "use strict" must be the first real statement in its scope.
Reveal explanation
The output is ReferenceError and then strict mode stays on inside that function. Because "use strict" appears at the top of assignmentCheck, assigning to the undeclared name accidentalGlobal is forbidden, so the catch block returns the error name. The directive affects that whole function scope and cannot be cancelled halfway through it.
Try it
Move "use strict"; below try { and run again. The directive will no longer be at the top of the function body, so you can see why placement matters, not just the text of the directive.
Practice
- Rewrite one example from this lesson without looking at the original, then run it and compare the result.
- Change one input, operator, method call, or option in a code sample and predict what will happen before running it.
- Explain The modern mode, "use strict" in your own words as if you were reviewing it with another learner.
Keep learning
Continue with Variables when you are ready for the next lesson.