Questions tagged [ecmascript-6]

The 2015 version of the ECMAScript specification, now a standard (ECMAScript 2015). Only use this tag where the question specifically relates to new features or technical changes provided in ECMAScript 2015.

ECMAScript 2015 (also known as ECMAScript 6) is the 2015 specification for the language (now superseded by the yearly released versions, managed by TC39). ES2015 adds significant updates to the language and its implementation in major JavaScript engines is almost complete (except legacy browsers, like Internet Explorer).

The tag or its alias should be used when your question covers one of the ES2015/ES6 features.

Related, transpiler specific tags are: ,


Features

  • Arrow functions
  • Classes
  • Enhanced Object Literals
  • Template Strings
  • Destructuring
  • Default + Rest + Spread
  • let + const
  • Iterators + for..of
  • Generators
  • Unicode
  • Modules
  • Module Loaders
  • Map + Set + Weakmap + Weakset
  • Proxies
  • Symbols
  • Subclassable Built-ins
  • Promises
  • Math + Number + String + Array + Object APIs
  • Binary and Octal Literals
  • Reflect API
  • Tail Calls
  • Typed arrays and DataViews

Useful Links

29895 questions
339
votes
17 answers

How can I conditionally import an ES6 module?

I need to do something like: if (condition) { import something from 'something'; } // ... if (something) { something.doStuff(); } The above code does not compile; it throws SyntaxError: ... 'import' and 'export' may only appear at the top…
ericsoco
  • 24,913
  • 29
  • 97
  • 127
336
votes
10 answers

ESLint: "error Parsing error: The keyword 'const' is reserved"

I am getting this error from ESLint: error Parsing error: The keyword 'const' is reserved from this code: const express = require('express'); const app = express(); const _ = require('underscore'); I've tried removing node_modules and…
opike
  • 7,053
  • 14
  • 68
  • 95
332
votes
6 answers

`export const` vs. `export default` in ES6

I am trying to determine if there are any big differences between these two, other than being able to import with export default by just doing: import myItem from 'myItem'; And using export const I can do: import { myItem } from 'myItem'; Are…
ajmajmajma
  • 13,712
  • 24
  • 79
  • 133
321
votes
5 answers

How to export imported object in ES6?

The use case is simple: I just want to export an object with the name just as it was imported. for example: import React from 'react'; export React; but this does not work. I have to write: import React from 'react'; export const React =…
Yao Zhao
  • 4,373
  • 4
  • 22
  • 30
306
votes
8 answers

ES6 exporting/importing in index file

I am currently using ES6 in an React app via webpack/babel. I am using index files to gather all components of a module and export them. Unfortunately, that looks like this: import Comp1_ from './Comp1.jsx'; import Comp2_ from './Comp2.jsx'; import…
MoeSattler
  • 6,684
  • 6
  • 24
  • 44
304
votes
1 answer

ES6 map an array of objects, to return an array of objects with new keys

I have an array of objects: [ { id: 1, name: 'bill' }, { id: 2, name: 'ted' } ] Looking for a simple one-liner to return: [ { value: 1, text: 'bill' }, { value: 2, …
Ben174
  • 3,654
  • 2
  • 17
  • 15
304
votes
45 answers

ReactJS giving error Uncaught TypeError: Super expression must either be null or a function, not undefined

I am using ReactJS. When I run the code below the browser says: Uncaught TypeError: Super expression must either be null or a function, not undefined Any hints at all as to what is wrong would be appreciated. First the line used to compile the…
Duke Dougal
  • 24,359
  • 31
  • 91
  • 123
299
votes
15 answers

Simplest way to merge ES6 Maps/Sets?

Is there a simple way to merge ES6 Maps together (like Object.assign)? And while we're at it, what about ES6 Sets (like Array.concat)?
jameslk
  • 4,298
  • 4
  • 21
  • 19
294
votes
2 answers

Should I use typescript? or I can just use ES6?

My daily job is client developer using AngularJS. We are evaluating if we want to go with TypeScript. I did some research on typescript and almost every JavaScript package I need must have definition type files. I found that is not very convenient,…
cjskywalker
  • 3,285
  • 2
  • 16
  • 16
293
votes
3 answers

bower init - difference between amd, es6, globals and node

I am creating my first Bower component. After running bower init the script asks me 'what types of modules does this package expose?' with these options: amd es6 globals node what is the difference between these options?
pherris
  • 17,195
  • 8
  • 42
  • 58
289
votes
1 answer

Node.js plans to support import/export ES6 (ECMAScript 2015) modules

I've been looking all over the Internet without a clear answer for this. Currently Node.js uses only CommonJS syntax to load modules, and if you really want to use the standard ECMAScript 2015 modules syntax, you either have to transpile it…
Zorgatone
  • 4,176
  • 4
  • 30
  • 47
281
votes
24 answers

Alternative for __dirname in Node.js when using ES6 modules

I use the flag --experimental-modules when running my Node application in order to use ES6 modules. However when I use this flag the metavariable __dirname is not available. Is there an alternative way to get the same string that is stored in…
Amygdaloideum
  • 3,683
  • 2
  • 13
  • 16
277
votes
5 answers

Concatenating variables and strings in React

Is there a way to incorporate React's curly brace notation and an href tag? Say we have the following value in the state: {this.state.id} and the following HTML attributes on a tag: href="#demo1" id="demo1" Is there a way I can add the id state to…
lost9123193
  • 10,460
  • 26
  • 73
  • 113
277
votes
12 answers

How can I find and update values in an array of objects?

I have an array of objects. I want to find by some field, and then to change it: var item = {...} var items = [{id:2}, {id:2}, {id:2}]; var foundItem = items.find(x => x.id == item.id); foundItem = item; I want it to change the original object.…
user3712353
  • 3,931
  • 4
  • 19
  • 33
274
votes
3 answers

Why es6 react component works only with "export default"?

This component does work: export class Template extends React.Component { render() { return (
component
); } }; export default Template; If i remove last row, it doesn't work. Uncaught TypeError:…
stkvtflw
  • 12,092
  • 26
  • 78
  • 155