14

I am curious to understand/figure-out if the ECMAScript-6 new-changes will work on the old browsers or not.

Why I am asking this question is:

I remember the introduction of 'use strict'; in ECMAScript-5, it was meant for the compatibility with the old versions.

That means the old browsers will keep working fine and they will just ignore it when they encounter the 'use strict'; statement while parsing the new JavaScript code.

And the new JS-engines will treat the statement 'use strict'; in some special way as detailed here Strict mode.


So, coming to the question

I seriously doubt and curious to know how would the ECMAScript-5 compliant browsers behave when they will parse the ECMAScript-6 code.

The reason for my doubt is ECMAScript-6 new features involve syntax change/updates. And the old browsers which are new-syntax-unaware-engines will start throwing errors when they encounter any of the new syntax from the following

yield[*], Map, Set, WeakMap, function* foo(){}, =>, for...of etc.

My concern is has the decision/inclusion of new features in ECMAScript-6 taken care of supporting the old-browsers without any break of code?

If Yes then how?

If Not then what should I do to keep my old-browser-users happy?

I see one solution to keep the users using old browsers happy by including some transpiler like traceur-compiler in my project. This will convert my ECMAScript-6 code to ECMAScript-5 equivalent. But do I have any other solution to keep my old-browser-users happy?

ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
dopeddude
  • 4,943
  • 3
  • 33
  • 41
  • 1
    Many ES6 features are not backward compatible and will only work in an ES6 JS engine. If your code runs in a browser, it will either only work in the newest browsers or you will have to wait until your entire browser support base is up to ES6 (years from now). If your code runs in something like node.js or if it's a plug-in for a specific version of a specific browser, then you have better control over the JS engine and can likely use ES6 features sooner. The different purpose of `"use strict";` is more consistent with allowing for backward compatibility. – jfriend00 May 10 '15 at 08:01
  • That's sad but seems true :( you can post the comment as your answer as well.... – dopeddude May 10 '15 at 08:08
  • Map, Set, and WeakMap can be polyfilled, and if you can avoid the new syntax, es6 code will work on es5... – dandavis May 10 '15 at 08:44
  • 1
    Also you should know that no effort were required to make 'use strict'; syntax error free on older engine as it just string declaration which anyway won't break any flow... – binariedMe Aug 20 '15 at 05:18
  • 'use strict' would not magically make ES5 features available in ES4 browsers, just like would not magically make HTML5 features available in HTML4 browsers. You and you alone is responsible for making your code and your site backward-compatible. – Sheepy Nov 24 '15 at 07:42
  • you have to plugin a transpiler to make your code compatible with older version browser. – Shami Qureshi Oct 04 '17 at 15:25

2 Answers2

9

What you are trying to explain here is forward compatibility. Obviously ES 5 ( or more precisely ES 5 engine ) is not forward compatible. Any way its hard to acheive and rare to find.

Although you can see that some of the features of upcoming ES 7 are already out and hence possibly ES 6's engine may be implemented considering those enhancement. So whenever ES 7 comes, some of the features will be working in older engine. Answer to the question, is ES 6 backward compatible will be "yes". Yes! ES 6's engine will be happy to run ES 5's code but vice versa is not true.

binariedMe
  • 4,309
  • 1
  • 18
  • 34
8

Many ES6 features will not work in an ES5 JS engine, particularly new syntax features such as for/of or arrow functions, generators, etc.... Some features like the Set object can be partially polyfilled for older browsers, others cannot.

Of the list of features you had in your question:

yield[*], Map, Set, WeakMap, function* foo(){}, =>, for...of

None of those are compatible with older versions of Javascript and will either cause syntax or reference errors. Some characteristics of Map and Set can be polyfilled (though not all). Yield, generators, arrow functions and for...of are just new syntax that older browsers do not process and cannot execute. It is possible to use an ES6 transpiler that will convert your code to ES5-compatible code. That isn't really ES6 backwards compatibility, but rather a code conversion that uses only ES5 syntax to accomplish the same things that are expressed in the newer ES6 syntax. Some of that is done with polyfills and some done with alternative ways of expressing an ES6 construct using only ES5 code (and usually more ES5 code).

If your code runs in something like node.js or if it's a plug-in for a specific version of a specific browser, then you have better control over the JS engine and can likely use ES6 features sooner than in a browser.

If your code runs in a browser and you're not using a transpiler to conver to ES5 code, then it will be awhile (many years) until most browsers in use on the internet are all ES6 ready.

The different purpose of "use strict"; (removing support for bad practices) is more consistent with allowing for compatibility with older versions than new language features like generators as the "use strict"; construct was specifically chosen to be something that a new browser could detect, but an older browser would just see as a normal string. New ES6 features that represent new language syntax are simply not that way as older browsers don't know how to process them and even if they could somehow ignore the newer syntax, they don't support the functionality that the new syntax implies.

You may find this article useful which discusses some of the issues in trying to use ES6 today:

ECMAScript 6 Resources For The Curious JavaScripter

If you want to use most of ES6 capabilities today in a wide range of browsers, then your best option is probably to transpile your code using something like BabelJS. This will transpile your ES6 code into ES5 compatible code that will run in any ES5 browser. You get to write in ES6, but the code will run in a wide range of browsers.

Or, if you're running in only a specific environment (such as a browser plug-in for a specific version of that browser) or a specific runtime engine such as node.js, then you can write code that uses the ES6 features that are already supported in that specific engine.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 5
    This answer is very misleading as it is using the term "backwards compatible" incorrectly. – ben Apr 26 '16 at 15:39
  • @ben - Please describe how you think this is using the term incorrectly. I'm happy to improve the answer, but don't quite understand what you mean. – jfriend00 Apr 26 '16 at 21:20
  • 2
    Sure. See the WIKI definition of backwards compatibility: https://en.wikipedia.org/wiki/Backward_compatibility ES6 is completely backwards compatible i.e. any ES5 program runs on ES6 as it is a superset of ES5 (AFAIK). – ben Apr 27 '16 at 21:21
  • 1
    @ben - It is the OP who used the term in that way. I was just answering the question that the OP was asking. I've removed the term entirely from my answer and I just speak about compatibility with older JS engines. You can tell pretty clearly from the first line of the OP's question what they intend to ask and what I've answered. – jfriend00 Apr 27 '16 at 22:31
  • 2
    Just because the OP uses a term wrong doesn't mean the answers should as well...Upvoted now after correction. – ben Apr 28 '16 at 11:10