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?

Shabith
  • 3,005
  • 3
  • 23
  • 20
  • The caution says it all, details in the article: http://msdn.microsoft.com/en-us/library/br230269(v=vs.94).aspx – user3348703 Feb 24 '14 at 22:07

2 Answers2

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).

SheetJS
  • 22,470
  • 12
  • 65
  • 75
  • 4
    ***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. – Jeremy J Starcher Aug 14 '13 at 09:22
  • @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? – SheetJS Aug 14 '13 at 09:27
  • 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. – Jeremy J Starcher Aug 14 '13 at 09:33
  • @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 correct – SheetJS Aug 14 '13 at 09:40
  • `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. – Jeremy J Starcher Aug 14 '13 at 09:47
  • 3
    @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](http://es5.github.io/#x11.2.1) expects an [*IdentifierName*](http://es5.github.io/#x7.6). – Jonathan Lonowski Aug 14 '13 at 20:00
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.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199