DEV Community

ES6 for beginners with example

Srebalaji Thirumalai on September 29, 2017

In this post, I will cover some new features in ES6. It will be helpful if you are new to ES6 or learning front-end frameworks. Topics I’m gonna c...
Collapse
 
eerk profile image
eerk • Edited

Thanks for this article! For get set I think it's more customary to use get name() and set name() instead of get getName() set setName(), but it's essentially the same.

class Person {
    constructor(name) {
      this._name = name;
    }
    get name() {
      return this._name;
    }
    set name(name) {
      this._name = name;
    }
}

let p = new Person("willie")
p.name = "jimmy"

console.log(p.name)
Collapse
 
srebalaji profile image
Srebalaji Thirumalai

Thank you. I updated :) :)

Collapse
 
reegodev profile image
Matteo Rigon

I'd like to point to the case of your get Name and set Name. Usually you keep those methods in lowercase to simulate an actual property so that who uses your class/object sees them as normal property retrieval/assignment.

@eerk example is perfect, since it is also good practice to name the actual property with the same name of the getter/setter methods prepended by an underscore

Collapse
 
kjellski profile image
Kjell Otto

Nice post and good examples, thanks! Although I don't understand why so many examples use let over const. I think it's better to use const wherever you can. Is there a reason to consider the opposite?

Collapse
 
reegodev profile image
Matteo Rigon

Because let will never throw an error and is seen as the successor of var, while const is more restrictive.
You are right though, as long as you write ES6 always use const unless you see you explicitly need to change the variable reference, like for counters and booleans

Collapse
 
booorkoo profile image
Borko Arsovic 🚀

Thanks for news,good post.

Collapse
 
zazulame profile image
Michael Zazula

In two of your examples I think you reference the wrong variable.

let SumElements = (arr) => {
console.log(arr); // [10, 20, 40, 60, 90]
let sum = 0;
for (let element of arr) {
sum += arr;
}
console.log(sum); // 220.
}
SumElements([10, 20, 40, 60, 90]);

As well as in the following example of for of loops.

Collapse
 
srebalaji profile image
Srebalaji Thirumalai

Thank you. I updated :) :)

Collapse
 
andrejnaumovski profile image
Andrej Naumovski

Overall nice post and clear explanations, however, arrow functions are not just a new syntax for writing a function in Javascript. Regular functions still exist, and both of them behave differently in a matter of ways, most importantly in how they affect the scope and how this is bound in each of them. So more explanation should be done on that part. But still, nice post!

Collapse
 
scunliffe profile image
Stephen Cunliffe

The case of the function examples is wrong, newOne and oldOne should start lowercase if you are trying to show good clean examples.

Collapse
 
srebalaji profile image
Srebalaji Thirumalai

Thank you. I updated :) :)

Collapse
 
rjschmertz_99 profile image
RJ Schmertz

Thanks for this. I have also put together some useful examples for using rest & spread in es6.

smartdevnow.com/2017/06/02/es6-res...

Collapse
 
hemanth profile image
hemanth.hm • Edited

Nice, you might like: jsfeatures.in and git.io/es6-paws

Collapse
 
quisutd3us profile image
David Salinas <FrontEnd Dev/>

Thnx You have the gift of explain the things easily... More post how this pls

Collapse
 
srebalaji profile image
Srebalaji Thirumalai

Thanks, @david . It means a lot to me. :)

Collapse
 
mifix profile image
Brian L Mayers

Thanks for the explanations, it cleared up some things in ES6 for me.

Collapse
 
richjdsmith profile image
Rich Smith

This is awesome dude, thanks for taking the time to make it!

Collapse
 
gregorgonzalez profile image
Gregor Gonzalez

I really appreciate your post. Didn't know about this. I love to see this big improvement and it's really useful. Now I want to learn more

Collapse
 
srebalaji profile image
Srebalaji Thirumalai

Thank you. It means a lot to me :) :)

Collapse
 
socramreysa profile image
Marcos Rey Sanguinetti

Genious

Collapse
 
vinnycheung profile image
vinnycheung

Nice post and thank you! The second expmple of 'Sets' is wrong in my editor, the keyword 'New' should be 'new'?

Collapse
 
oscarcentenor profile image
Oscar Centeno

Great explanation. Thanks!

Collapse
 
alvarocaceresmu profile image
Álvaro Cáceres Muñoz

Conclusion: the whole article is self-explanatory.