Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test if ES6 is ~fully supported
var supportsES6 = function() {
try {
new Function("(a = 0) => a");
return true;
}
catch (err) {
return false;
}
}();
@bendc

This comment has been minimized.

Copy link
Owner Author

bendc commented Aug 25, 2016

Load your untranspiled ES6 file for modern browsers:

if (supportsES6) {
  var script = document.createElement("script");
  script.src = "my-es6-file.js";
  document.head.appendChild(script);
}
@rik

This comment has been minimized.

Copy link

rik commented Aug 25, 2016

I think this requires some performance testing since you can't benefit from browsers "parse ahead" behaviour to download the JS file as soon as possible.

@triblondon

This comment has been minimized.

Copy link

triblondon commented Aug 26, 2016

The ~ is pretty critical here, unless we know that for every browser, the last part of ES6 to be supported was either arrows or default args?

@triblondon

This comment has been minimized.

Copy link

triblondon commented Aug 26, 2016

@rik optimising for the modern browsers, you could <link rel=preload> it

@bendc

This comment has been minimized.

Copy link
Owner Author

bendc commented Aug 26, 2016

@triblondon Yup, the critical test is the a = 0 (all browsers supporting default parameters have a fairly complete support of ES6 -- for example, Edge 13 will be rejected by this test despite a decent ES6 coverage).

@kenberkeley

This comment has been minimized.

Copy link

kenberkeley commented Mar 1, 2019

<html>
<head>
  <script>
  if (typeof Symbol === 'undefined') {
    document.write('<script crossorigin="anonymous" src="https://polyfill.io/v3/polyfill.min.js?features=es6,es7&flags=gated"><\/script>')
  }
  </script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.