Strict mode in JavaScript

What is strict mode?

Strict mode was introduced in ECMAScript5 also known as ES5 in 2009.
Adding strict mode to JavaScript code allows your code to be checked more strictly.
Allows programmer to write more secure JavaScript code.


This content originally appeared on DEV Community and was authored by Abhishek Rath

What is strict mode?

  • Strict mode was introduced in ECMAScript5 also known as ES5 in 2009.
  • Adding strict mode to JavaScript code allows your code to be checked more strictly.
  • Allows programmer to write more secure JavaScript code.

Benefits of Strict Mode

  • Using strict mode in JavaScript code throws visible errors in developer console which would otherwise fail silently (shown in the examples below).
  • Forbids programmers to use certain syntax.
  • In some cases using strict mode in code can make the code to run faster.

How to enable strict mode in JavaScript?

  • To enable strict mode write "use strict" or 'use strict' at the beginning of JavaScript code.
    Strict mode for entire code

  • The strict mode can also be used for a function.

  • To enable strict mode in function, "use strict" or 'use strict' must be the very first line in function body.
    Strict mode in function

Common mistakes that occur in non-strict mode

Using reserved words as variable names

Image description

Output 20

  • Wait!! Isn't "let" a reserved word in the JavaScript? Therefore it mustn't be used as a variable name according to naming an identifier rules. Here in the above code snippet JavaScript fails silently instead of throwing an error.
  • But, if we use strict mode, JavaScript engine will throw an error- Strict mode in code snippet Produces error

Using variables before declaring them

variable used before declaration Output

  • Using strict mode gives Reference Error.
    Variable used before declaration in strict mode
    Output

  • To know more about the strict mode, checkout the documentation by MDN

Conclusion

  • That will be all from my side. I hope, this article gave you a basic idea about the strict mode.

Keep Learning!!


This content originally appeared on DEV Community and was authored by Abhishek Rath


Print Share Comment Cite Upload Translate Updates
APA

Abhishek Rath | Sciencx (2021-10-14T19:25:49+00:00) Strict mode in JavaScript. Retrieved from https://www.scien.cx/2021/10/14/strict-mode-in-javascript/

MLA
" » Strict mode in JavaScript." Abhishek Rath | Sciencx - Thursday October 14, 2021, https://www.scien.cx/2021/10/14/strict-mode-in-javascript/
HARVARD
Abhishek Rath | Sciencx Thursday October 14, 2021 » Strict mode in JavaScript., viewed ,<https://www.scien.cx/2021/10/14/strict-mode-in-javascript/>
VANCOUVER
Abhishek Rath | Sciencx - » Strict mode in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/14/strict-mode-in-javascript/
CHICAGO
" » Strict mode in JavaScript." Abhishek Rath | Sciencx - Accessed . https://www.scien.cx/2021/10/14/strict-mode-in-javascript/
IEEE
" » Strict mode in JavaScript." Abhishek Rath | Sciencx [Online]. Available: https://www.scien.cx/2021/10/14/strict-mode-in-javascript/. [Accessed: ]
rf:citation
» Strict mode in JavaScript | Abhishek Rath | Sciencx | https://www.scien.cx/2021/10/14/strict-mode-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.