Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Revert "2.3"#85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Revert "2.3"#85
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
78 changes: 41 additions & 37 deletions1-js/02-first-steps/03-strict-mode/article.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,89 @@ | ||
#The modern mode, "use strict" | ||
For a long time,JavaScriptevolved 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 whenECMAScript 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: | ||
```js | ||
"use strict"; | ||
//this code works the modern way | ||
... | ||
``` | ||
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. | ||
````warn header="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: | ||
```js no-strict | ||
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"`. | ||
```` | ||
```warn header="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](info:devtools) 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`key:Shift+Enter`to input multiple lines, and put`use strict`on top, like this: | ||
```js | ||
'use strict'; <Shift+Enterfor a newline> | ||
// ...your code | ||
<Enterto 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: | ||
```js | ||
(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. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.