What's your favorite addition to ES2015+

twitter logo 惻1 min read

If you're unfamiliar with ECMAScript I have an explanation here

I'd love to hear about everyone's favorite piece of ES2015+ syntax and how it makes your JavaScript development experience better!

I'll start with mine, destructuring assignment! I love that I can do this

({data}) => {
    data.item
}

Instead of this

(data) => {
    data.data.item
}
twitter logo DISCUSS (36)
markdown guide
 

Using the spread operator to append/prepend elements to an array. It also returns the array rather than the length of the array, which I've never found that useful.


let numbers = [1, 2, 3]

numbers = [...numbers, 4]

// [1, 2, 3, 4]

 

Spread operator is wonderful. What's crazy to me is how many different ways it can be used. Like arrays vs. objects.

 

oh man, Object spread is the best thing... maybe I should change my answer šŸ¤”

 

I wish there could be such an easy way to remove elements from array.

 

Async/await is so great. No more callback hell ever again.

 

First time I used Async/Await was a Vue project with a Java Servlet backend. Plopped await in front of the fetch that connected with my back end, and async in front of vue's mounted() method and squealed in delight when my component rendered with my data base information for the first time.

Multiple moments of triumph at once, first use Async/Await, first success with a servlet, and was still new to Vue.JS.

 
 

Let's say, you need to sequentially resolve three promises. Callback hell looks like this:

function asyncJob(callback) {
  asyncA(xyz, (err, buffer) => {
    if (err) { throw err; }
    asyncB(buffer, (err, result) => {
      if (err) { throw err; }
      asyncC(result, (err, response) => {
        if (err) { throw err; }
        if (response.ok()) { return callback(); }
        throw new Error('Could not send result');
      });
    })
  });
}

Now the same with Promises:

const asyncJob = () => asyncA(xyz)
.then((buffer) => asyncB(buffer))
.then((result) => asyncC(result))
.then((response) => {
  if (response.ok()) { return Promise.resolve(); }
  throw new Error('Could not send result');
});

Reads a lot better, doesn't it? Still, it's pretty terse and has a lot of repetition, so async/await is a big improvement when it comes to reading flow:

const asyncJob = async () => {
  const buffer = await asyncA(xyz);
  const result = await asyncB(buffer);
  const response = await asyncC(result);
  if (!response.ok()) {
    throw new Error('Could not send result');
  }
};

We call the second thenable hell.
I'm a fan of Promise.all as a solution, but I acknowledge async/await is a great option too.

.then(...) still takes a callback. So while it reads much more congruently, it's basically still good old callback hell, just in a better suit and with better manners. Async/await is a game changer when it comes to readability of asynchronous code.

 
 

Arrow functions. I love the clean, simple syntax for managing functions. We can go from this:

const add = function(a, b) {
  return a + b;
}

To just this:

const add = (a, b) => a + b;

There are learning and readability curves here, but it's not bad and I think well worth it.

 
 

Definitely destructuring, spread, arrow functions, template literals for string interpolation, generators (even if I'm not currently using them), array includes, async/await. Curious to see how async interation works

 

You can't have 5 favorites rhymes! Just kidding, love it.

 

hahahah ok, so if I have to choose one I'm going to say destructuring.
You can live without spread, arrow functions, literals, generators, includes and asnc await.

Destructuring in function arguments makes the code much more readable instead of having either a super long list of parameters or options as an argument.

Destructuring FTW

 

Destructuring

So before ES2015 and destructuring we had this:

var width = this.state.width;
var height = this.state.height;
var position = this.state.position;

or for arrays

var width = this.state[0];
var height = this.state[1];
var position = this.state[2];

Now it's prettier with

const { width, height, position } = this.state;

or for arrays:

const [ width, height, position ] = this.state;

It comes really handy when iterating over Object.entries, which gives you an array like:

[ [key1, value1], [key2, value2] . . . . [keyN, valueN] ]

Because you can do this:

const entries = Object.entries(obj);
for([key, value] of entries) {
   // do something with the key and value
}

Or this

Object.entries(obj).filter(([key, value]) => {
    if(value > 10)
       return key;
});

It's nice, like python packing/unpacking:

value1, value2 = sys.argv[1:2]

I mean, sure it can get ugly:

const {
   width,
   height,
   position,
   color,
   font,
   kerning,
   linespacing,
   border,
   background,
   textcontent
} = this.state;

but that goes for anything in programming.

 
 
 

I am looking forward to the up and coming class field syntax to fix the broken syntax that is class syntax. Syntax šŸ˜‚

 

Oh yes, definitely an awesome addition.

 

Ooooph tough decision... probably the combination of proper native iterators, the for ... of loop, and generators, which all combine together to amazing effect.

 

I watched an awesome talk on generators last week. Not something Iā€™d used before!

 

They're super cool and can really clean up your code in some cases, especially when you get into async iterators/generators. My second article ever on dev.to was actually about iterators, generators, symbols, for ... of, and how it all fits together!

 
 
 

But I said metaprogramming so that is the category that proxies sit under .. anyway here's how to break JavaScript with proxies.

 
 

native support for import/export šŸ˜‚ and certainly those bacticks

`Hello ${World}`

and

styled.div`
  color: blue
`
 

Spread, destructuring, arrow functions, async-await - they made me actually see JavaScript for what it is instead of just a browser scripting language šŸ˜ƒ now I'm just waiting for the pipeline operator

 
 

Metaprogramming for sure! It's probably not healthy to have that much power though, I still love it.

 
 

The easy arrow function syntax to make small function really short and less cramy like eg:

 const square= num => num*num 

instead of old pre ES-6 syntax
Classic DEV Post from Jun 20

Do you use time-tracking for work or for your personal time?

Laurie profile image
Software dev at Ten Mile Square | DC techie | Conference speaker | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter

šŸ‘‹ Hey dev.to reader.

Do you prefer sans serif over serif?

You can change your font preferences in the "misc" section of your settings. ā¤ļø