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
266
votes
3 answers

What is "function*" in JavaScript?

In this page I found a new JavaScript function type: // NOTE: "function*" is not supported yet in Firefox. // Remove the asterisk in order for this code to work in Firefox 13 function* fibonacci() { // !!! this is the interesting line !!! let…
string QNA
  • 3,110
  • 3
  • 17
  • 14
259
votes
13 answers

forEach is not a function error with JavaScript array

I'm trying to make a simple loop: const parent = this.el.parentElement console.log(parent.children) parent.children.forEach(child => { console.log(child) }) But I get the following error: VM384:53 Uncaught TypeError: parent.children.forEach is…
alexchenco
  • 53,565
  • 76
  • 241
  • 413
259
votes
8 answers

How do I write a named arrow function in ES2015?

I have a function that I am trying to convert to the new arrow syntax in ES6. It is a named function: function sayHello(name) { console.log(name + ' says hello'); } Is there a way to give it a name without a var statement: var sayHello =…
jhamm
  • 24,124
  • 39
  • 105
  • 179
258
votes
7 answers

Object.hasOwnProperty() yields the ESLint 'no-prototype-builtins' error: how to fix?

I am using the following logic to get the i18n string of the given key. export function i18n(key) { if (entries.hasOwnProperty(key)) { return entries[key]; } else if (typeof (Canadarm) !== 'undefined') { try { throw Error(); }…
booYah
  • 2,589
  • 2
  • 10
  • 4
252
votes
8 answers

Transforming a Javascript iterable into an array

I'm trying to use the new Map object from Javascript EC6, since it's already supported in the latest Firefox and Chrome versions. But I'm finding it very limited in "functional" programming, because it lacks classic map, filter etc. methods that…
Stefano
  • 18,083
  • 13
  • 64
  • 79
251
votes
5 answers

How to map/reduce/filter a Set in JavaScript?

Is there any way to map/reduce/filter/etc a Set in JavaScript or will I have to write my own? Here's some sensible Set.prototype extensions Set.prototype.map = function map(f) { var newSet = new Set(); for (var v of this.values())…
Mulan
  • 129,518
  • 31
  • 228
  • 259
251
votes
24 answers

Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables?

The typical way to loop x times in JavaScript is: for (var i = 0; i < x; i++) doStuff(i); But I don't want to use the ++ operator or have any mutable variables at all. So is there a way, in ES6, to loop x times another way? I love Ruby's…
at.
  • 50,922
  • 104
  • 292
  • 461
246
votes
11 answers

Wrap long template literal line to multiline without creating a new line in the string

In es6 template literals, how can one wrap a long template literal to multiline without creating a new line in the string? For example, if you do this: const text = `a very long string that just continues and continues and continues` Then it will…
Ville Miekk-oja
  • 18,749
  • 32
  • 70
  • 106
243
votes
6 answers

How to convert a plain object into an ES6 Map?

For some reason I can't find this simple thing in the MDN docs (maybe I'm just missing it). I expected this to work: const map = new Map({foo: 'bar'}); map.get('foo'); // 'bar' ...but the first line throws TypeError: (var)[Symbol.iterator] is not…
callum
  • 34,206
  • 35
  • 106
  • 163
242
votes
19 answers

Unable to access React instance (this) inside event handler

I am writing a simple component in ES6 (with BabelJS), and functions this.setState is not working. Typical errors include something like Cannot read property 'setState' of undefined or this.setState is not a function Do you know why? Here is…
user3696212
  • 3,381
  • 5
  • 18
  • 31
233
votes
11 answers

Null-safe property access (and conditional assignment) in ES6/2015

Is there a null-safe property access (null propagation / existence) operator in ES6 (ES2015/JavaScript.next/Harmony) like ?. in CoffeeScript for example? Or is it planned for ES7? var aThing = getSomething() ... aThing = possiblyNull?.thing This…
P Varga
  • 19,174
  • 12
  • 70
  • 108
232
votes
33 answers

In React ES6, why does the input field lose focus after typing a character?

In my component below, the input field loses focus after typing a character. While using Chrome's Inspector, it looks like the whole form is being re-rendered instead of just the value attribute of the input field when typing. I get no errors from…
spunge
  • 2,827
  • 3
  • 14
  • 12
223
votes
23 answers

How to load external scripts dynamically in Angular?

I have this module which componentize the external library together with additional logic without adding the