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
437
votes
12 answers

Access to ES6 array element index inside for-of loop

We can access array elements using a for-of loop: for (const j of [1, 2, 3, 4, 5]) { console.log(j); } How can I modify this code to access the current index too? I want to achieve this using for-of syntax, neither forEach nor for-in.
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
427
votes
13 answers

What does the @ mean inside an import path?

I'm starting out a new vue.js project so I used the vue-cli tool to scaffold out a new webpack project (i.e. vue init webpack). As I was walking through the generated files I noticed the following imports in the src/router/index.js file: import Vue…
Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
427
votes
8 answers

What is the motivation for bringing Symbols to ES6?

UPDATE: Recently a brilliant article from Mozilla came up. Read it if you're curious. As you may know they are planning to include new Symbol primitive type in ECMAScript 6 (not to mention some other crazy stuff). I always thought that the :symbol…
Yanis
  • 4,847
  • 2
  • 17
  • 17
423
votes
4 answers

TypeScript export vs. default export

What is the difference in TypeScript between export and default export? In all the tutorials, I see people exporting their classes and I cannot compile my code if I don't add the default keyword before exporting. Also, I couldn't find any trace of…
fos.alex
  • 5,317
  • 4
  • 16
  • 18
404
votes
20 answers

Declaring static constants in ES6 classes?

I want to implement constants in a class, because that's where it makes sense to locate them in the code. So far, I have been implementing the following workaround with static methods: class MyClass { static constant1() { return 33; } static…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
397
votes
11 answers

where is create-react-app webpack config and files?

I create a ReactJS project with the create-react-app package and that worked well, but I cannot find webpack files and configurations. How does react-create-app work with webpack? Where are the webpack configuration files located in a default…
Mohammad
  • 4,441
  • 3
  • 13
  • 15
391
votes
8 answers

When to use ES6 class based React components vs. functional ES6 React components?

After spending some time learning React I understand the difference between the two main paradigms of creating components. My question is when should I use which one and why? What are the benefits/tradeoffs of one over the other? ES6…
omarjmh
  • 13,632
  • 6
  • 34
  • 42
387
votes
17 answers

Programmatically navigate using react router V4

I have just replaced react-router from v3 to v4. But I am not sure how to programmatically navigate in the member function of a Component. i.e in handleClick() function I want to navigate to /path/some/where after processing some data. I used to do…
Colin Wang
  • 6,778
  • 5
  • 26
  • 42
366
votes
8 answers

Can I use ES6's arrow function syntax with generators? (arrow notation)

That is, how do I express function *(next) {} with arrow syntax? I've tried all the combinations I could think of, and I can't find any documentation on it. (I am currently using Node.js v0.11.14.)
Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81
364
votes
8 answers

Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

I understand why var takes that name - it is variable, const - it is a constant, but what is the meaning behind the name for let, which scopes to the current block? Let it be?
Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
360
votes
14 answers

Is it possible to import modules from all files in a directory, using a wildcard?

With ES6, I can import several exports from a file like this: import {ThingA, ThingB, ThingC} from 'lib/things'; However, I like the organization of having one module per file. I end up with imports like this: import ThingA from…
000
  • 26,951
  • 10
  • 71
  • 101
354
votes
16 answers

How do you JSON.stringify an ES6 Map?

I'd like to start using ES6 Map instead of JS objects but I'm being held back because I can't figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be listed. Do I really have to write a wrapper…
rynop
  • 50,086
  • 26
  • 101
  • 112
350
votes
7 answers

Are variables declared with let or const hoisted?

I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected... console.log(typeof name); // undefined var name = "John"; ...variables declared with let or const seem to have some problems…
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
348
votes
24 answers

How to import a JSON file in ECMAScript 6?

How can I access a JSON file in ECMAScript 6? The following doesn't work: import config from '../config.json' This works fine if I try to import a JavaScript…
Nikita Jajodia
  • 4,220
  • 3
  • 21
  • 29
342
votes
5 answers

Proper use of const for defining functions

Are there any limits to what types of values can be set using const in JavaScript, and in particular, functions? Is this valid? Granted it does work, but is it considered bad practice for any reason? const doSomething = () => { ... } Should all…
David Sinclair
  • 4,187
  • 3
  • 17
  • 12