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
6164
votes
40 answers

What is the difference between "let" and "var"?

ECMAScript 6 introduced the let statement. I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently than the var keyword. What are the differences? When should let be used instead of var?
TM.
  • 108,298
  • 33
  • 122
  • 127
2669
votes
29 answers

Can (a== 1 && a ==2 && a==3) ever evaluate to true?

Moderator note: Please resist the urge to edit the code or remove this notice. The pattern of whitespace may be part of the question and therefore should not be tampered with unnecessarily. If you are in the "whitespace is insignificant" camp, you…
Dimpu Aravind Buddha
  • 9,505
  • 4
  • 17
  • 23
1300
votes
11 answers

Using Node.js require vs. ES6 import/export

In a project I am collaborating on, we have two choices on which module system we can use: Importing modules using require, and exporting using module.exports and exports.foo. Importing modules using ES6 import, and exporting using ES6 export Are…
kpimov
  • 13,632
  • 3
  • 12
  • 18
1125
votes
12 answers

When should I use curly braces for ES6 import?

It seems to be obvious, but I found myself a bit confused about when to use curly braces for importing a single module in ES6. For example, in the React-Native project I am working on, I have the following file and its content: File…
TonyW
  • 18,375
  • 42
  • 110
  • 183
1106
votes
33 answers

"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

I'm using ArcGIS JSAPI 4.12 and wish to use Spatial Illusions to draw military symbols on a map. When I add milsymbol.js to the script, the console returns error Uncaught SyntaxError: Cannot use import statement outside a module` so I add…
Jerry Chen
  • 11,595
  • 5
  • 13
  • 16
868
votes
12 answers

What is "export default" in JavaScript?

File: SafeString.js // Build out our basic SafeString type function SafeString(string) { this.string = string; } SafeString.prototype.toString = function() { return "" + this.string; }; export default SafeString; I have never seen export…
damphat
  • 18,246
  • 8
  • 45
  • 59
857
votes
2 answers

How can I alias a default import in JavaScript?

Using ES6 modules, I know I can alias a named import: import { foo as bar } from 'my-module'; And I know I can import a default import: import defaultMember from 'my-module'; I'd like to alias a default import and I had thought the following would…
sfletche
  • 47,248
  • 30
  • 103
  • 119
845
votes
12 answers

Call async/await functions in parallel

As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallel. So, for example, we have this code: await…
Victor Marchuk
  • 13,045
  • 12
  • 43
  • 67
837
votes
6 answers

ECMAScript 6 arrow function that returns an object

When returning an object from an arrow function, it seems that it is necessary to use an extra set of {} and a return keyword because of an ambiguity in the grammar. That means I can’t write p => {foo: "bar"}, but have to write p => { return {foo:…
Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99
799
votes
4 answers

Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?

Arrow functions in ES2015 provide a more concise syntax. Can I replace all my function declarations / expressions with arrow functions now? What do I have to look out for? Examples: Constructor function function User(name) { this.name =…
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
733
votes
8 answers

What do multiple arrow functions mean in JavaScript?

I have been reading a bunch of React code and I see stuff like this that I don't understand: handleChange = field => e => { e.preventDefault(); /// Do something here }
jhamm
  • 24,124
  • 39
  • 105
  • 179
654
votes
40 answers

How to add multiple classes to a ReactJS Component?

I am new to ReactJS and JSX and I am having a little problem with the code below. I am trying to add multiple classes to the className attribute on each li:
  • My React component…
    Hector
    • 6,625
    • 3
    • 16
    • 18
    654
    votes
    10 answers

    What's the difference between "super()" and "super(props)" in React when using es6 classes?

    When is it important to pass props to super(), and why? class MyComponent extends React.Component { constructor(props) { super(); // or super(props) ? } }
    Misha Moroshko
    • 166,356
    • 226
    • 505
    • 746
    648
    votes
    25 answers

    How can I clone a JavaScript object except for one key?

    I have a flat JS object: {a: 1, b: 2, c: 3, ..., z:26} I want to clone the object except for one element: {a: 1, c: 3, ..., z:26} What's the easiest way to do this (preferring to use es6/7 if possible)?
    fox
    • 15,428
    • 20
    • 55
    • 85
    620
    votes
    37 answers

    How to update nested state properties in React

    I'm trying to organize my state by using nested property like this: this.state = { someProperty: { flag:true } } But updating state like this, this.setState({ someProperty.flag: false }); doesn't work. How can this be done correctly?
    Alex Yong
    • 7,425
    • 8
    • 24
    • 41
    1
    2 3
    99 100