3

I know some basics of JavaScript and new to ES6.

I just tried a simple ES6 feature import and export and it seems that it doesn't support browser. I searched and came to know that we must use any transpiler(babel) to convert the ES6 to ES5.

So, the import/export converted to CommonJs syntax. If it is like this then I can directly use CommonJs!?

  1. If ES6 is ready then why latest browser doesn't support yet?
  2. If Ihave to convert my ES6 to ES5 then why I have to code on ES6? I can direclty use ES5!

Index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="main.js" type="text/javascript"></script>
</head>
<body>    
</body>
</html>

main.js

import {square} from 'func';
console.log( square(6));

func.js

export function square(x) {
    return x * x;
}

chrome error for Index.html Chrome error - unexpected token import

chrome version chrome verson - 59.0.3071.86

Update

It's not duplicate of any answered questions, the referenced one was answered 5 years back. We are in JavaScript where every Monday we get new news ;)

3
  • from the docs: This feature is only beginning to be implemented in browsers natively at this time. It is implemented in many transpilers, such as the Traceur Compiler, Babel, Rollup or Webpack. Commented Jun 12, 2017 at 12:16
  • @michał-perłakowski It's not duplicate, that question was answered 5 years back. We are in JavaScript where every month we get new things... Commented Jun 12, 2017 at 12:21
  • 1
    @M.Kumaran And when we haven't gotten this particular thing for 5 years, it's still a duplicate :-) Commented Jun 12, 2017 at 13:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.