1

Since modern browsers support ES6. Do they internally transpiles ES6 down to ES5, and then execute the code? Or they can understand ES6 code natively using C++ engine?

If they can run ES6 directly, how does it ensure the behaviour of executing ES6 code is absolutely equivalent to executing the transpiled ES5 version of that code?

Joseph Stuard
  • 69
  • 1
  • 5
  • 1
    Native code will be running ES6 directly,. They will not be worried about ES5, they will be following the spec.. https://262.ecma-international.org/6.0/ – Keith May 13 '21 at 23:41
  • 2
    Whether or not the transpiled code does the same that "native" ES6 code would do, is not the concern of the browser. It's the concern of the transpiler. If they behave differently, then it's a bug in the transpiler. The browser just runs whatever code you give it. – Szabolcs Dézsi May 13 '21 at 23:42
  • The latest spec is ECMAScript 2020 https://262.ecma-international.org/, so that's ES11? Since moving to a yearly spec, it's more important about _which features_ each browser supports, not which version. – evolutionxbox May 13 '21 at 23:46
  • 1
    It's not the JS engine's responsibility to make sure the ES6 runs the same as ES5 transpiled code - that's the transpiler's responsibility. The JS engine needs to run ES6 code to the spec. A JS engine does not internally transpile ES6 down to ES5 - it executes ES6 natively. A TypeScript capable engine such as Deno does internally transpile the TypeScript to ESx and then runs the ESx (where `x` denotes whatever its current version of JS support is). There are no runtime engines I'm aware of that support TypeScript natively. There are lots of JS engines that support ES6 natively. – jfriend00 May 13 '21 at 23:47
  • Is babel always able to transpile new ES features into ES5? i.e. could it be some scenarios where a new JS feature is not imitable via ES5? – Joseph Stuard May 15 '21 at 21:46
  • @JosephStuard There's lots of limitations, as [mentioned in the docs](https://babeljs.io/docs/en/caveats). Maybe most notably, [proxies are not available in ES5](https://stackoverflow.com/q/35025204/1048572), just like getters/setters are not available in ES3. – Bergi May 15 '21 at 23:16

1 Answers1

1

The answer to your question is simple: No, browser doesn't transpile your code.

There is no need because new specification try to stay backward compatible by adding new syntactic sugar, new functions and new apis, keeping the existing stuff as is.

EmeraldCoder
  • 527
  • 3
  • 12
  • Is babel always able to transpile new ES features into ES5? i.e. could it be some scenarios where a new JS feature is not imitable via ES5? – Joseph Stuard May 15 '21 at 21:46
  • There is some edge cases list in this page of Babel documentation: https://babeljs.io/docs/en/caveats – EmeraldCoder May 16 '21 at 13:55