JavaScript is an incredibly fast and efficient programming language to use for a variety of purposes. Today Every type of software uses JavaScript, including Web apps, 3D games, robots, IoT devices, etc.
Back in 2007, Jeff Atwood (founder of StackOverflow), made a case that JavaScript would become a bigger part of web development. Atwood coined the term Atwoodโs Law
, which states:
Any application that can be written in JavaScript, will eventually be written in JavaScript.
Itโs now ten years later, and Atwoodโs statement is truer than ever. JavaScript is continuing to gain more and more adoption. The โnext generationโ of Javascript is something known as ES6. ES6 is so far the best and biggest update javascript has ever received. It streamlined the javascript development and almost killed jQuery's career. ES6 mainly allows you to write less code and do more.
In this post, I'll go over the six major differences between ES6 and ES5. Letโs take a look.
Arrow function: write less do more
ES5 WAY
function add(a,b){
return a+b
}
console.log(add(2,3))
//OUTPUT: 5
ES6 WAY
const add = (a,b)=> a+b
console.log(add(2,3))
//OUTPUT: 5
Use const if you don't want to reassign the 'element variable by mistake.
ES5 WAY
var element = document.getElementById('myForm')
ES6 WAY
const element= document.getElementById('myForm')
De-structuring: write less do more!
ES5 WAY
var user = {
name "Ritesh Kumar",
username: "@0xRitesh"
}
const name = user.name
const username user.username
ES6 WAY
var user = {
name "Ritesh Kumar".
username: "@0xRitesh"
}
const {name,username} = user
Template Literals
ES5 WAY
function getUsertMessage(name,country){
console.log('Hi, my name is '+ name+ ',and I am from '+ country)
}
logUserMessage('Ritesh, 'India')
ES6 WAY
function logUserMessage(name,country){
console.log(`Hi, my name is ${name}, and I am from ${country}`)
}
logUserMessage('Ritesh', 'India')
improve Object Literals
ES5 WAY
function getUserObj ( name, age, address){
return {
name: name,
age: age,
address: address
}
}
ES6 WAY
function getUserObj ( name, age, address){
return {
name,
age,
address
}
}
Default Parameters
ES5 WAY
function ES5Fun( username, platform){
username, = username, || '@wordssaysalot';
platform = platform || 'Dev';
}
ES6 WAY
function ES6Fun( username = '@wordssaysalot' , platform= 'Dev') {
}
Conclusion
Thanks for reading the article! I hope you guys found this article useful, and I hope I was able to introduce you to some of the ES6 features.
Top comments (12)
You have a few typos:
In "Arrow function: write less do more", ES6 is missing an
=
:Also ideally you should show how it works with a single argument:
In "Template Literals" you used
'
instead of using back-ticks:One last thing: You go back and forward between using new and old syntax. Ideally you should use arrow functions in all your examples.
Cheers!
Sometimes for the sake of simplicity you don't want to use arrow function syntax.
E.g. event listeners, the reference of *this * comes in handy to reference the element. On the other hand you could also use event.target. My point is, arrow function syntax is not the holy grail.
If you write arrow functions inside classes, you can access
this
:And in plain objects you can still avoid writing
function
by using this syntax:Arrow functions aren't the holy grail, indeed ... but that doesn't mean that we need to use
function
everywhere either, that only means that we need to use it only when arrow functions don't work.Thank you for pointing this up; it was a typo. and I've made the necessary changes.
Remember: Arrow function does not have their own scope so use
this will be
undefined
in modules andwindow
in page scriptsdev.to/lukeshiru/comment/1kmgb
Dude he used backticks. Check well.
The post was updated after I made the comment ๐
Note for Arrow functions:
Arrow functions can be tricky when using it without declaration.
Error would be:
But with declaration
Looks like this:
Instead of this
const add (a,b)=> a+b
it should be
const add = (a,b)=> a+b
good read