Every concept in Javascript has too much information to keep in mind. Even "use strict;"
as well. I can write lengthy article on it but I am a kind of guy usually ignore lengthy theoretical articles. So, this time I want to write article in different way but like one stop for "use strict;"
. No further words. Let's start:
What is "use strict;"
?
To indicate code should run in strict mode
- Thatz it. Yes 😊
How to use?
Everywhere in article writing it as "use strict;"
. Just place the same to the beginning of function or script.
When it is introduced in Javascript?
Do we really required?🧐 anyhow, In ES 5.
Please don't ask me, who introduced it. 😛
why do I use?
It makes to write secure
Javascript. Short answers are not accepted for questions starting with Why
😳.
Alright, Let's go deep but no theory.
S.No | Action | Example | Strict Mode | Non - Strict Mode | Fix | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Using variable without declaring it |
This file contains 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
|
Uncaught ReferenceError: a is not defined | 3.14 |
This file contains 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
|
||||||||||||||||||||||||||||||||||||||||||
2 | Deleting a variable |
This file contains 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
|
Uncaught SyntaxError: Delete of an unqualified identifier in strict mode. | false | It can't be deleted. Variables created without var, let & const are deleted using delete. | ||||||||||||||||||||||||||||||||||||||||||
3 | Duplicate function params |
This file contains 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
|
Uncaught SyntaxError: Duplicate parameter name not allowed in this context | 10 10 |
This file contains 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
|
||||||||||||||||||||||||||||||||||||||||||
4 | Octal numeric literals |
This file contains 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
|
Uncaught SyntaxError: Octal literals are not allowed in strict mode. | 8 |
This file contains 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
|
||||||||||||||||||||||||||||||||||||||||||
5 | Using variable name as eval or arguments |
This file contains 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
|
Uncaught SyntaxError: Unexpected eval or arguments in strict mode | 10 20 |
This file contains 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
|
||||||||||||||||||||||||||||||||||||||||||
6 | `this` inside function - refers to the object that called the function. |
This file contains 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
|
undefined | Returns window object: Window {0: Window, 1: Window, 2: global,...... | Avoid using this inside functions to not to expose window object. | ||||||||||||||||||||||||||||||||||||||||||
7 | Creating variable with `eval` |
This file contains 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
|
Uncaught ReferenceError: a is not defined | 10 | Avoid using eval. | ||||||||||||||||||||||||||||||||||||||||||
8 | Using `with` statement |
This file contains 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
|
Uncaught SyntaxError: Strict mode code may not include a with statement | 20 | Avoid using it | ||||||||||||||||||||||||||||||||||||||||||
9 | Deleting undeletable property |
This file contains 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
|
Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] } at :2:1 | false | Avoid it | ||||||||||||||||||||||||||||||||||||||||||
10 | Using Octal Escape characters |
This file contains 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
|
Uncaught SyntaxError: Octal escape sequences are not allowed in strict mode. | Returns nothing | Avoid it | ||||||||||||||||||||||||||||||||||||||||||
11 | Writing to read only property |
This file contains 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
|
Uncaught TypeError: Cannot assign to read only property 'a' of object '#' | 10 |
This file contains 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
|
||||||||||||||||||||||||||||||||||||||||||
12 | Writing to get only property |
This file contains 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
|
Uncaught TypeError: Cannot set property test of # which has only a getter | FIRST |
This file contains 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
|
||||||||||||||||||||||||||||||||||||||||||
13 | Using variable as reserved keywords |
This file contains 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
|
Uncaught SyntaxError: Unexpected strict mode reserved word | 10 | Avoid using reserved keywords as variables |
Oops, it took whole day to write. Anyhow, please do comment if I miss any or in case of improvements.
Thanks.
💎 Love to see your response
- Like - You reached here means. I think, I deserve a like.
- Comment - We can learn together.
- Share - Makes others also find this resource useful.
- Subscribe / Follow - to stay up to date with my daily articles.
- Encourage me - You can buy me a Coffee
Let's discuss further.
- Just DM @urstrulyvishwak
-
Or mention
@urstrulyvishwak
Top comments (0)