DEV Community

Make Your Code Cleaner, Shorter and Easier to Read! ES6 Tips and Tricks.

Sam Williams on January 23, 2018

Template literals Template literals make working with string so much easier than before. They're started with a back tick and can have v...
Collapse
 
4rontender profile image
Rinat Valiullov

In console I have: "Uncaught SyntaxError: Unexpected token." (row#2 => job.company:...)

function es6({ age, name, job.company: company}) {
var yearOfBirth = 2018 - age,
console.log(
${ name } works at ${ company } and was born in ${ yearOfBirth }.);
}

Collapse
 
peril profile image
Erica DiGiulio

That's because of the comma at the end of the assignment.

Collapse
 
dshepsis profile image
dshepsis

The code sample for Dynamic Property Names is incorrect:

let  city= 'sheffield_';
let a = {
    [ city + 'population' ] = 350000
};
a[ city + 'county' ] = 'South Yorkshire';
console.log(a); // {sheffield_population: 350000, sheffield_county: 'South Yorkshire' }

On line 3, you use an = symbol instead of the : operator.

Collapse
 
samwsoftware profile image
Sam Williams

Thanks for pointing that out. All changed now

Collapse
 
faabiopontes profile image
Fabio Pontes

Nice article, nicely summary of ES6 Tips and Tricks. The for has a typo.

for (var i = 1, i < 5, i++){ // for is separated with ; instead of ,
    setTimeout(() => { console.log(i); }, 1000);
}

Correct one:

for (var i = 1; i < 5; i++){
    setTimeout(() => { console.log(i); }, 1000);
}
Collapse
 
samwsoftware profile image
Sam Williams

Thanks, I must have been on auto pilot!

Collapse
 
4rontender profile image
Rinat Valiullov • Edited

You have a tiny misspelling


function foo() {
return {
name: 'Anna',
age: 56,
job: { company: 'Tesco', title: 'Manager' }
};
}
// pre ES6
let a = foo(), name = a.name, *****age = name.age*****, company = a.job.company;
// ES6 destructuring and concise parameters
let { name, age, job.company: company} = foo();

Collapse
 
samwsoftware profile image
Sam Williams

Thanks for pointing it out.

Collapse
 
reggietheroman profile image
reggietheroman

Theres so many 'little' things here I have seen in my senior developers code that I've never been able to wrap my head around! Thanks so much for sharing!

Collapse
 
samwsoftware profile image
Sam Williams

It's great when you learn a new thing and then suddenly understand how a bit of code works. Hopefully you can now write code more like a senior developer!

Collapse
 
gutem profile image
Gutem

2 typos:

var fName = 'Peter', sName = 'Smith', age = 43, job: 'photographer';
var a = 'Hi, I'm ' + fName + ' ' + sName + ', I'm ' + age + ' and work as a ' + job + '.';

Change the ":" at job assigment to "=" and escape all single quotes inside the var a or use double quotes starting/finishing.

var fName = 'Peter', sName = 'Smith', age = 43, job = 'photographer';
var a = 'Hi, I\'m ' + fName + ' ' + sName + ', I\'m ' + age + ' and work as a ' + job + '.';
Collapse
 
samwsoftware profile image
Sam Williams

Thanks, the dev.to community is so observant

Collapse
 
vkorotynskyy profile image
Vitaliy Korotynskyy • Edited

Great ES-6 features explanation. I've found typo:

function foo(a, b, c) { console.log(a=${a}, b=${b}, c=${c}}
let data = [5, 15, 2];
foo( ...data); // a=5, b=15, c=2

There is a missing closing bracket of console.log()

Collapse
 
samwsoftware profile image
Sam Williams

Thanks for pointing that out. I hope you enjoyed the post beside the typos

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
samwsoftware profile image
Sam Williams

If you would like to, that would be amazing.

Collapse
 
plainjavascript profile image
plainJavaScript

Really nice little Tutorial. You always went directly to the main point. I saw many tutorials that looks like a philosophical stuff...

Collapse
 
samwsoftware profile image
Sam Williams

Thanks. That's what I've been aiming for. There are so many books and sites out there explaining the exact mechanics and all possible variations but that doesn't help many people.

Is there another topic you think I should try to tackle?

Collapse
 
skyberx profile image
skyberx

can you please share the color scheme you used for python notebook? thanks :)

Collapse
 
samwsoftware profile image
Sam Williams

The colour scheme here is the one that Dev.to use with their markdown compiler. You'd have to ask them

Collapse
 
samwsoftware profile image
Sam Williams

Thanks Alex, I'll change it now.

Collapse
 
machy44 profile image
machy44

Yeah I saw that too. There should be console.log(c) because b is still [1,2,3,4];

P.S. Awesome article from Author.