Why do we still have to use quoted string literal to switch on strict in JS? Surely something a little more strongly 'typed' could be used here, like calling a built in function, say, Object.UseStrict()or something like that. What is the reason behind having to resort to a string literal?
Asked
Active
Viewed 1,095 times
22
ProfK
- 46,970
- 114
- 378
- 743
-
8It's succinct and won't cause errors on browsers that don't support it. – Waxen Dec 12 '14 at 17:21
-
I didn't think it a duplicate of "What does 'use strict' do" because I know what it does. I was asking specifically about its 'syntax'. – ProfK Dec 12 '14 at 17:26
-
I wondered the same thing and I agree that it's not a duplicate. The linked article doesn't explain _why_ this is the way it's turned on. – Sam Rueby Apr 05 '18 at 18:25
1 Answers
24
Compatibility across all browsers and JS runtime engines.
E.g., http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
No new syntax is introduced in order to enable strict mode. This is huge. This means that you can turn strict mode on in your scripts – today – and it’ll have, at worst, no side effect in old browsers.
Dave Newton
- 156,572
- 25
- 250
- 300