'use strict'
I have worked in Node codebases every day for the last couple of years. One of the statements you see littered throughout files is 'use strict'. One of the purposes of this blog is for me to highlight the areas that I have always assumed without questioning.
'use script' enables a mode called "Strict Mode." This mode has been developed by the ECMAScript5 specification. It essentially serves to put additional security and clear syntax measures in front of your code. I'll go through a couple examples of what 'use strict' changes
Restricting undeclared variables
If I use 'use strict' in a js file and do not declare a variable, I will get an error:
Gotchas:
- 'use strict' is only readable by browsers at the top of a file.
- It can be declared for an entire script, or for a function specifically. Probably best to use in an entire script and not mix and match.
- With use strict, var numeric assignments can be read as octal (i.e. - var a = 030 would be the value 24)
'use script' enables a mode called "Strict Mode." This mode has been developed by the ECMAScript5 specification. It essentially serves to put additional security and clear syntax measures in front of your code. I'll go through a couple examples of what 'use strict' changes
Restricting undeclared variables
If I use 'use strict' in a js file and do not declare a variable, I will get an error:
No More Implied Global Variables
OOB Js allows for Global Variables - meaning you can declare a variable in one file and use it in another without properly instantiating as a new object in the second file. This just makes proper prototypal inheritance cleaner.
Fail Loud, Fail Fast
Chances are refactoring an application to 'use strict' is going to throw some errors, given the ways that JS has just swallowed some improper actions in the past. While this might take more time to refine, it will ultimately result in cleaner, more explicitly defined code.
Gotchas:
- 'use strict' is only readable by browsers at the top of a file.
- It can be declared for an entire script, or for a function specifically. Probably best to use in an entire script and not mix and match.
- With use strict, var numeric assignments can be read as octal (i.e. - var a = 030 would be the value 24)

Comments
Post a Comment