Is it safe to use 'use strict' in IE 8/9
Asked Answered
A

2

9

According the this http://caniuse.com/use-strict 'use strict' does not support in IE version 8/9.

My question is, Is it really safe to use 'use strict' in IE 8/9 or browsers that its not compatible with? Will it break my code?

Aras answered 14/8, 2013 at 4:57 Comment(1)
The caution says it all, details in the article: msdn.microsoft.com/en-us/library/br230269(v=vs.94).aspxHimalayas
G
18

The statement "use strict"; will should not cause problems with IE8/9 insofar as the browsers will run the code. (It was designed that way, to ensure that there are no problems with browsers that don't implement strict mode)

External source: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

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.

NOTE: as Jeremy pointed out in the comments, there are some expressions which are technically valid but will fail in IE8 (for example: var x = {}; x.break = true will not work in IE8 even though it will work in IE9).

Gunplay answered 14/8, 2013 at 5:3 Comment(6)
Mostly true. There is some syntax allowed in strict mode that will fail outside of strict mode. In particular, using keywords as object properties. myObject.break = true works in strict mode, fails outside of it.Envoi
@JeremyJStarcher it appears that var x = {}; x.break = true works in IE9 (just tested it out in f12). Do you have access to IE9 and can you verify?Gunplay
Fails in IE7 (Quirks Mode) -- Use the developer tools to change that and retry. My point is that strict mode isn't always 100% compatible with non-strict mode.Envoi
@JeremyJStarcher It works in IE7 (Quirks), but fails in IE7 (IE7 standards). Interestingly enough, x["break"] works. I wonder if that's due to some inconsistency between ES3 and ES5. Regardless, you are technically correctGunplay
x["break"] works in ES3, but ES3 doesn't let you use dot notation if the property name is a keyword. Silly carryover that ES5 got rid of. It is an edge case that few people realize, but it has bitten me more than once working on various teams. I figure that some ES5 engines don't bother checking that rule in strict/non-strict mode.Envoi
@JeremyJStarcher myObject.break = true is only a SyntaxError in ES3-based engines/browsers (IE 8 and older). ES5 in general allows it, whether or not strict mode is used. Reserved words are only disallowed in ES5 when an Identifier is expected, while dot notation expects an IdentifierName.Telson
T
3

Yeah, it should be fine.

use directives are meant to be backwards-compatible. Browsers that don't support them will just see a String literal that isn't referenced further. So, they'll pass over it and move on.

Though, you'll still want to be sure that you test your code both with and without it enabled.

Telson answered 14/8, 2013 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.