Why is "use strict" still a string literal? [duplicate]
Asked Answered
H

1

26

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?

Harmonia answered 12/12, 2014 at 17:18 Comment(2)
It's succinct and won't cause errors on browsers that don't support it.Because
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'.Harmonia
G
27

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.

Grinnell answered 12/12, 2014 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.