+ 4

Strict mode

What will happen if we don't write "strict mode" in js.

29th Jan 2019, 1:42 AM
Abhishek Shailendra Khandare
Abhishek Shailendra Khandare - avatar
9 Answers
+ 5
It make writing JavaScript more strict. So more rules to follow or errors.
29th Jan 2019, 2:22 AM
Rowsej
Rowsej - avatar
+ 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.
29th Jan 2019, 2:27 AM
Schindlabua
Schindlabua - avatar
+ 4
Nothing will happen.
29th Jan 2019, 1:45 AM
Rowsej
Rowsej - avatar
+ 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.
29th Jan 2019, 2:55 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 3
So why its needed?
29th Jan 2019, 1:47 AM
Abhishek Shailendra Khandare
Abhishek Shailendra Khandare - avatar
+ 2
Strict does not allow hoisting.
29th Jan 2019, 10:54 PM
Syeda Zobia Kanwal
Syeda Zobia Kanwal - avatar
+ 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?
29th Jan 2019, 3:46 AM
James
James - avatar
+ 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.
29th Jan 2019, 3:48 AM
Rowsej
Rowsej - avatar