What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?

In JavaScript, the use strict statement allows us to choose strict mode to write and execute our code.

Normal JS is very beginner friendly.

It tolerates semantic errors by remaining silent about them – which can result in unnoticed bugs.
It does a l…


This content originally appeared on DEV Community and was authored by Abdullah Numan

In JavaScript, the use strict statement allows us to choose strict mode to write and execute our code.

Normal JS is very beginner friendly.

  • It tolerates semantic errors by remaining silent about them - which can result in unnoticed bugs.
  • It does a lot of heavy lifting for mapping variables with their identifiers by checking the scope chain for each name - which costs time and memory.
  • It makes life easier by treating the parameter values set at function definition to be the same as the values passed to the function at invocation as items of the arguments object - which can sometimes render actual passed in values unimportant.
  • It autoboxes the this value of a function and exposes the Function.prototype.caller and Function.prototype.arguments APIs that gives access to the caller function and arguments object respectively. All of these three poses security concerns.

Strict Mode

Strict mode addresses these issues and brings about changes to give developers more control over their code. The changes can be classified into four catergories. Below we briefly discuss some of them in each category. For detailed explanation and code examples, please refer to this excellent MDN article

1. Changes related to mistakes arising from syntax and type conversion

Mistakes related to syntax and type conversion throw errors, instead of silently ignoring them. There are several of them.
For example,

  • mistyped variables throw ReferenceError.
  • Assignment to a non-writable global (like undefined or NaN) throw a TypeError.

Please refer to this section of the MDN Strict Mode article for more examples.

2. Changes related to variable usage

  • Variable name mapping is optimized by prohibiting the use of with.
  • eval can introduce new variables in it's own enclosed scope only, not in the surrounding / global scope.
  • Deleting declared variables is not allowed.

3. Changes related to eval and arguments object

  • eval and arguments object are made easier to work with. They are treated like other pre-assigned language keywords and cannot be used to name variables and functions.
  • arguments object of a function is set only when the function is invoked. So setting a value for an argument in the function definition does not update the arguments object and updating an item in the arguments object with arguments[i] = 'Something, not necessarily a string' does not change the value of the corresponding parameter variable.

4. Changes related to security

  • Written code is made more secure by preventing autoboxing of this. undefined and null values of this do not autobox to the Global object.
  • Function.prototype.caller and Function.protoype.arguments throw TypeError, so this prevents traversing the call stack - making strict mode code more secure.

References

  1. Strict Mode
  2. What does "use strict" do in JavaScript, and what is the reasoning behind it?


This content originally appeared on DEV Community and was authored by Abdullah Numan


Print Share Comment Cite Upload Translate Updates
APA

Abdullah Numan | Sciencx (2022-07-18T05:08:18+00:00) What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?. Retrieved from https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/

MLA
" » What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?." Abdullah Numan | Sciencx - Monday July 18, 2022, https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/
HARVARD
Abdullah Numan | Sciencx Monday July 18, 2022 » What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?., viewed ,<https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/>
VANCOUVER
Abdullah Numan | Sciencx - » What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/
CHICAGO
" » What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?." Abdullah Numan | Sciencx - Accessed . https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/
IEEE
" » What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file?." Abdullah Numan | Sciencx [Online]. Available: https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/. [Accessed: ]
rf:citation
» What is the significance, and what are the benefits, of including ‘use strict’ at the beginning of a JavaScript source file? | Abdullah Numan | Sciencx | https://www.scien.cx/2022/07/18/what-is-the-significance-and-what-are-the-benefits-of-including-use-strict-at-the-beginning-of-a-javascript-source-file/ |

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.