Es5 Vs Es6 (With Example Code) - Codeburst
Es5 Vs Es6 (With Example Code) - Codeburst
7K
You can see the di erence we don’t have to use the function keyword to
de ne the function now but that’s now a big improvement right ?
Another thing to notice that we don’t have to write the return keyword to
return the computed value, in ES6 if you don’t write function body inside
braces the computed expression automatically get returned when the function
will be executed.
var obj1 = { a: 1, b: 2 }
var obj2 = { a: 2, c: 3, d: 4}
var obj3 = Object.assign(obj1, obj2)
We have to merge the object using Object.assign() which takes both objects
as input and outputs the merged object. Let’s take a look how we can tackle
this problem in ES6.
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 2, c: 3, d: 4}
const obj3 = {...obj1, ...obj2}
Top highlight
Simple isn’t it ? The spread operator makes merging objects a breeze for the
developer.
Let’s take a look at object destructuring now. If you have to extract multiple
values from ES5 you have to write 3–4 lines of code like this:
var obj1 = { a: 1, b: 2, c: 3, d: 4 }
var a = obj1.a
var b = obj1.b
var c = obj1.c
var d = obj1.d
const obj1 = { a: 1, b: 2, c: 3, d: 4 }
const {
a,
b,
c,
d
} = obj1
Cool! Last but not least look at another new feature for introduced for objects.
var a = 1
var b = 2
var c = 3
var d = 4
var obj1 = { a: a, b: b, c: c, d: d }
var a = 1
var b = 2
var c = 3
var d = 4
var obj1 = { a, b, c, d }
Yeah, if the name of the key and the variable we are going to assign to that
key are same you can use this shorthand.
Promises vs Callbacks
Javascript is an Async language we all know that. This feature gives us a lot of
freedom when we write code. We have a non-blocking architecture in our
hand because of which we can write non-dependent code easily.
In the next piece of code we call the isGreater function pass it a and b
alongside our callback function. Inside callback function we check if the result
is true or false and shows message according to it. Now let’s see how ES6
have handled this.
The ES6 promises are better then callback because it allows us to distinguish
easily between a success and error so we don’t have to check again for
things in our callback function.
module.exports = myModule;
The ES6 syntax is more readable here. Alongside the export keyword ES6
also comes up with export default later on this rstly let’s take a look how
importing a module changed in ES6.
Cool ha !!
Okay so as promised now let’s talk about the export default . When you
export something using default we will import a module like this.
The above line means something like this, we exported a module by default
and we have to import that whole module in your source le.
But ES6 also provides us with an ability to export and import multiple
child modules or variables from a single module.
So in your module le you will export your module something like this
export const x = 1;
export const y = 2;
export const z = 'String';
Pretty neat ha ? Here we used ES6 object destruction to import multiple child
modules from one single module.
. . .
Hi, My name is Manoj Singh Negi. I am a Javascript Developer and writer follow
me at Twitter or Medium.
3. Be an Artist.
Peace.
1.7K 9
Responses
Write a response…
Kushan Joshi
Dec 6, 2017 · 1 min read
Manoj this a great article! Liked the way you showed es5 vs es6 in a concise
manner.
There are some tiny issues, which I guess you can correct easily.
Read more…
26
Joshua S
Jan 17
75 1 response