How can I find out the browser's Javascript engine version and support to ECMAScript 6?
I'm using navigator.appVersion
just to know the browser's version, but not the engine's version.
How can I find out the browser's Javascript engine version and support to ECMAScript 6?
I'm using navigator.appVersion
just to know the browser's version, but not the engine's version.
For now there's not a exact way to detect ES6, but if you test its features in the current browser, you can determine if the engine is ES6. My esx library detects the ECMAScript version by doing syntax tests and methods check. For know it can detect ECMAScript 3, 5, 6 and 7 (ES7 not tested, but should work), if no ECMAScript test matched, it gives
null
as result.Example using my library:
Put the incompatible syntax code, such as containing arrow functions, in it's own script block and polyfill it with compatible syntax code.
Feature detection
I suggest you to use feature detection instead of detecting the browser's engine with heuristic methods. To do this you can simply wrap some code inside a
try {..} catch (e) {...}
statement, or use someif (...)
statements.For example:
Why is feature detection better than browser/engine detection?
There are multiple reasons that make, in most of the cases, feature detection the best option:
You don't have to rely on browser's version, engine or specifics, nor detect them using heuristic methods which are hard and pretty crafty to implement.
You will not fall into errors regarding browser/engine specifications detection.
You don't have to worry about browser-specific features: for example WebKit browsers have different specifications than other ones.
You can be sure that, once a feature is detected, you'll be able to use it.
These are the main reasons that IMHO make feature detection the best approach.
Feature detection + fallback
When using feature detection, a pretty smart way to work when you aren't sure which features you can/cannot use consists in several feature detections and consequent fallbacks to more basic methods (or even creation of these methods from scratch) in case the features you want to use are not supported.
A simple example of feature detection with fallback may be applied to the
window.requestAnimationFrame
feature, which is not supported by all the browsers, and has several different prefixes depending on the browser you're working on. In this case, you can easily detect and fallback like this:ECMAScript 6 (Harmony) features detection
Now, coming to the real problem: if you want to detect the support to ES6, you'll not be able to behave like I said above, because a relevant range of ES6 features is based on new syntaxes and private words, and will throw a
SyntaxError
if used in ES5, which means that writing a script which contains both ES5 and ES6 is impossible!Here is an example to demonstrate this issue; the below snippet won't work, and it will be blocked before execution because contains illegal syntax.
Now, since that you cannot both check and execute ES6 conditionally in the same script, you'll have to write two different scripts: one which only uses ES5, and another one which includes ES6 features. With two different scripts you'll be able to import the ES6 one only if it is supported, and without causing
SyntaxErrors
to be thrown.ES6 detection and conditional execution example
Now let's make a more relatable example, and let's say you want to use these features in your ES6 script:
Symbol
objectsclass
keyword(...)=>{...}
) functionsNOTE: feature detection of newly introduced syntaxes (like arrow functions) can only be done using the
eval()
function or other equivalents (e.g.Function()
), because writing invalid syntax will stop the script before its execution. This is also the reason why you cannot useif
statements to detect classes and arrow functions: these features are regarding keywords and syntax, so aneval(...)
wrapped inside atry {...} catch (e) {...}
block will work fine.So, coming to the real code:
HTML Markup:
Code in your
es5script.js
script:Code in your
es6script.js
:Browser/engine detection
Like I said above, browser and engine detection are not the best practices when programming some JavaScript script. I'm gonna give you some background on this topic, just not to leave my words as a "random personal opinion".
Quoting from the MDN Documentation [link]:
Also, you're saying you use
navigator.appVersion
, but consider using another approach, because that one, together with many other navigator properties, is deprecated, and doesn't always behave like you think.So, quoting from the MDN Documentation [link] again:
As Marco Bonelli said, the best way to detect ECMAScript 6 language syntax is to use eval();. If the call does not throw an error, "all other" features are supported, but I recommend Function();.
demo: https://jsfiddle.net/uma4Loq7/
Browser vendors that support ES6 modules now provide an easy way to do feature detection:
The script with the
nomodule
attribute will not be excuted by browsers which support<script type="module" ...>
You can also inject the script like this: