+ 4
Strict mode
What will happen if we don't write "strict mode" in js.
9 Answers
+ 5
It make writing JavaScript more strict. So more rules to follow or errors.
+ 5
The designers of javascript made some bad decisions. Now 20 years later there are a few things we would like to change about js but we can't because it would break old code.
So as a result there is strict mode and you use these changes without affecting old code.
The advantages are that browsers can optimize strict code better, leading to faster programs.
Strict mode also helps the programmer catch some bugs which would have gone unnoticed in sloppy mode.
You usually don't notice when programming so it doesn't hurt to enable strict mode.
The list of changes can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
For example in sloppy mode, 010 == 8 because of "octal notation". Strict mode disables it because nobody needs it anyway.
+ 4
Nothing will happen.
+ 4
If the codes run well in strict mode, it would be no issue to run without strict mode.
However if the code has some non-strict mode, accepted mistakes like variable without defined with var or let
E.g.
'use strict' ;
a = 1; // instead of var a = 1;
There would be an error show in strict mode, but no error shown if the code run without 'use strict'.
It's advisable to use strict mode to write better JavaScript codes.
+ 3
So why its needed?
+ 2
Strict does not allow hoisting.
+ 1
I also have some questions about this.
Where should I put the statement for strict mode? What do I write? When is it necessary?
+ 1
James You write "use strict;" (with quotes) into the top of your JS code. Or if you only want it to apply for a function, type it as the first line of that function.