Questions tagged [ecmascript-2016]

Questions about new features specified in the ECMAScript 2016 specification.

This tag targets ECMAScript 2016, a JavaScript standard previously known as ECMAScript 7. Any proposal that reached stage 4 by Jan 28th, 2016 became part of the new version.

These are:

440 questions
461
votes
11 answers

How to iterate (keys, values) in JavaScript?

I have a dictionary that has the format of dictionary = {0: {object}, 1:{object}, 2:{object}} How can I iterate through this dictionary by doing something like for((key,value) in dictionary){ //Do stuff where key would be 0 and value…
nbroeking
  • 6,010
  • 3
  • 15
  • 38
423
votes
26 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
  • 11,694
  • 20
  • 47
  • 76
175
votes
8 answers

Using `window.location.hash.includes` throws “Object doesn't support property or method 'includes'” in IE11

I am checking the URL to see if it contains or includes a ? in it to control the hash pop state in the window. All other browsers aren’t having an issue, only IE. The debugger gives me this error when I try to load in this way: Object doesn't…
Erik Grosskurth
  • 3,309
  • 4
  • 22
  • 40
138
votes
6 answers

How can I `await` on an Rx Observable?

I'd like to be able to await on an observable, e.g. const source = Rx.Observable.create(/* ... */) //... await source; A naive attempt results in the await resolving immediately and not blocking execution Edit: The pseudocode for my full intended…
CheapSteaks
  • 4,091
  • 3
  • 28
  • 46
132
votes
7 answers

Array.prototype.includes vs. Array.prototype.indexOf

Beyond the improved readability, is there any advantage to includes over indexOf? They seem identical to me. What is the difference between this var x = [1,2,3].indexOf(1) > -1; //true And this? var y = [1,2,3].includes(1); //true
Matt
  • 3,680
  • 5
  • 21
  • 33
119
votes
1 answer

Why is Math.pow() (sometimes) not equal to ** in JavaScript?

I've just discovered the ECMAScript 7 feature a**b as an alternative for Math.pow(a,b) (MDN Reference) and came across a discussion in that post, in which they apparently behave differently. I've tested it in Chrome 55 and can confirm that the…
Thomas Altmann
  • 1,639
  • 1
  • 9
  • 16
91
votes
6 answers

Best way to polyfill ES6 features in React app that uses create-react-app

I've been testing my React.js application on internet explorer, and finding that some ES6/7 code like Array.prototype.includes() breaks it. I'm using create-react-app, and apparently they've chosen not to include a lot of polyfills since not…
68
votes
6 answers

Can you use es6 import alias syntax for React Components?

I'm trying to do something like the following, however it returns null: import { Button as styledButton } from 'component-library' then attempting to render it as: import React, { PropTypes } from "react"; import cx from 'classNames'; import {…
Jim
  • 1,676
  • 2
  • 13
  • 21
62
votes
3 answers

Succinct/concise syntax for 'optional' object keys in ES6/ES7?

There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript: const obj = { requiredKey1: ..., requiredKey2: ... }; if (someCondition) { obj.optionalKey1 =…
Andrew Mao
  • 31,800
  • 17
  • 126
  • 212
61
votes
2 answers

ES2015/2016 way of 'typeof varName === 'undefined`?

I'm wallowing in ES2015+ luxury with a few projects right now and am wondering whether I can get rid of the much hated crutch to check for undefined in the new wonderland. Is there a shorter but still exact way to typeof varName === 'undefined' in…
Hedge
  • 13,498
  • 35
  • 122
  • 223
57
votes
2 answers

Why is -1**2 a syntax error in JavaScript?

Executing it in the browser console it says SyntaxError: Unexpected token **. Trying it in node: > -1**2 ... ... ... ...^C I thought this is an arithmetic expression where ** is the power operator. There is no such issue with other…
psmith
  • 1,742
  • 15
  • 18
57
votes
2 answers

React with ES7: Uncaught TypeError: Cannot read property 'state' of undefined

I'm getting this error Uncaught TypeError: Cannot read property 'state' of undefined whenever I type anything in the input box of AuthorForm. I'm using React with ES7. The error occurs on 3rd line of setAuthorState function in ManageAuthorPage.…
Khpalwalk
  • 849
  • 1
  • 6
  • 11
56
votes
7 answers

IE does not support Array includes or String includes methods

I have been working on a project and developing a JavaScript framework. The original code is about 700 lines so I only pasted this line. The includes method doesn't work on Internet Explorer. Is there any solution for this? var row_cells =…
37
votes
2 answers

VSCode Linter ES6 ES7 Babel linter

How to use Visual Studio code to lint JavaScript file based on babel/ES7 stage-0 rules? I only need to lint code. I already have webpack transpiling Js file.
35
votes
3 answers

How do I destructure all properties into the current scope/closure in ES2015?

I'd like to do something like this: const vegetableColors = {corn: 'yellow', peas: 'green'}; const {*} = vegetableColors; console.log(corn);// yellow console.log(peas);// green I can't seem to find or figure out how to do this but I really…
Resist Design
  • 4,133
  • 2
  • 18
  • 34
1
2 3
29 30