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
618
votes
17 answers

Map vs Object in JavaScript

I just discovered this feature: Map: Map objects are simple key/value maps. That confused me. Regular JavaScript objects are dictionaries, so how is a Map different from a dictionary? Conceptually, they're identical (according to another question…
Dave
  • 44,275
  • 12
  • 65
  • 105
599
votes
16 answers

Object spread vs. Object.assign

Let’s say I have an options variable and I want to set some default value. What’s is the benefit / drawback of these two alternatives? Using object spread options = {...optionsDefault, ...options}; Or using Object.assign options = Object.assign({},…
Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
560
votes
41 answers

Private properties in JavaScript ES6 classes

Is it possible to create private properties in ES6 classes? Here's an example. How can I prevent access to instance.property? class Something { constructor(){ this.property = "test"; } } var instance = new…
d13
  • 9,817
  • 12
  • 36
  • 44
544
votes
17 answers

ES6 class variable alternatives

Currently in ES5 many of us are using the following pattern in frameworks to create classes and class variables, which is comfy: // ES 5 FrameWork.Class({ variable: 'string', variable2: true, init: function(){ }, addItem:…
wintercounter
  • 7,500
  • 6
  • 32
  • 46
527
votes
19 answers

Node.js - SyntaxError: Unexpected token import

I don't understand what is wrong. Node v5.6.0 NPM v3.10.6 The code: function (exports, require, module, __filename, __dirname) { import express from 'express' }; The error: SyntaxError: Unexpected token import at exports.runInThisContext…
SofDroid
  • 5,450
  • 2
  • 13
  • 16
519
votes
19 answers

Getting Unexpected Token Export

I am trying to run some ES6 code in my project but I am getting an unexpected token export error. export class MyClass { constructor() { console.log("es6"); } }
Jason
  • 5,247
  • 2
  • 10
  • 3
515
votes
8 answers

What are the actual uses of ES6 WeakMap?

What are the actual uses of the WeakMap data structure introduced in ECMAScript 6? Since a key of a weak map creates a strong reference to its corresponding value, ensuring that a value which has been inserted into a weak map will never disappear as…
valderman
  • 8,365
  • 4
  • 22
  • 29
513
votes
3 answers

module.exports vs. export default in Node.js and ES6

What is the difference between Node's module.exports and ES6's export default? I'm trying to figure out why I get the "__ is not a constructor" error when I try to export default in Node.js 6.2.2. What works 'use strict' class SlimShady { …
Marty Chang
  • 6,269
  • 5
  • 16
  • 25
513
votes
30 answers

Adding script tag to React/JSX

I have a relatively straightforward issue of trying to add inline scripting to a React component. What I have so far: 'use strict'; import '../../styles/pages/people.scss'; import React, { Component } from 'react'; import DocumentTitle from…
ArrayKnight
  • 6,956
  • 4
  • 17
  • 20
511
votes
14 answers

What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?

I know that the >= operator means more than or equal to, but I've seen => in some source code. What's the meaning of that operator? Here's the code: promiseTargetFile(fpParams, aSkipPrompt, relatedURI).then(aDialogAccepted => { if…
rpgs_player
  • 5,369
  • 3
  • 15
  • 18
496
votes
7 answers

How to convert Map keys to array?

Lets say I have the following map: let myMap = new Map().set('a', 1).set('b', 2); And I want to obtain ['a', 'b'] based on the above. My current solution seems so long and horrible. let myMap = new Map().set('a', 1).set('b', 2); let keys =…
Lilleman
  • 7,392
  • 5
  • 27
  • 36
496
votes
9 answers

When should I use arrow functions in ECMAScript 6?

With () => {} and function () {} we are getting two very similar ways to write functions in ES6. In other languages lambda functions often distinguish themselves by being anonymous, but in ECMAScript any function can be anonymous. Each of the two…
490
votes
31 answers

Filter object properties by key in ES6

Let's say I have an object: { item1: { key: 'sdfd', value:'sdfd' }, item2: { key: 'sdfd', value:'sdfd' }, item3: { key: 'sdfd', value:'sdfd' } } I want to create another object by filtering the object above so I have something like. { …
29er
  • 8,595
  • 12
  • 48
  • 65
469
votes
10 answers

How can I mock an ES6 module import using Jest?

I want to test that one of my ES6 modules calls another ES6 module in a particular way. With Jasmine this is super easy -- The application code: // myModule.js import dependency from './dependency'; export default (x) => { …
Cam Jackson
  • 11,860
  • 8
  • 45
  • 78
439
votes
30 answers

How to scroll to an element?

I have a chat widget that pulls up an array of messages every time I scroll up. The problem I am facing now is the slider stays fixed at the top when messages load. I want it to focus on the last index element from the previous array. I figured out…
edmamerto
  • 7,605
  • 11
  • 42
  • 66