DEV Community

Cover image for JavaScript ES6
Buddhadeb Chhetri
Buddhadeb Chhetri

Posted on • Updated on

JavaScript ES6

JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.

ECMAScript is the official name of the language.

ECMAScript versions have been abbreviated to ES1, ES2, ES3, ES5, and ES6.

Since 2016 new versions are named by year (ECMAScript 2016 / 2017 / 2018).

Arrow Function

const sum =(a,b)=> a+b
console.log(sum(2,6))
//prints 8
Enter fullscreen mode Exit fullscreen mode

Default parameters

function print(a=5){
console.log(a)
}
print();
//prints 5
Enter fullscreen mode Exit fullscreen mode

Let Scope

let a=3;
if(true){
let a=5;
console.log(a);//prints 5
}
console.log(a);//prints 3
Enter fullscreen mode Exit fullscreen mode

Const

//can be assigned only once
var x = 50;
// Here x is 50
{
  const x = 16;
  console.log(x);// Here x is 16
}
console.log(x); // Here x is 50
Enter fullscreen mode Exit fullscreen mode

Multiline String

console.log(`This is a 
multiline string`);
Enter fullscreen mode Exit fullscreen mode

Template string

const name ='Chhetri'
const message =`Buddhadeb ${name}`
console.log(message)
//Prints Buddhadeb Chhetri
Enter fullscreen mode Exit fullscreen mode

Exponent Operator

const byte =2 ** 8
console.log(byte)
//same as : Math.pow(2,8)
Enter fullscreen mode Exit fullscreen mode

Spread Operator

const a=[1,2]
const b=[3,4]
const c=[...a,...b]
console.log(c) 
//[1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

String Includes()

console.log('apple'.includes('p'))
//Prints true
console.log('apple'.includes('tt'))
//prints false
Enter fullscreen mode Exit fullscreen mode

String StartsWith()

console.log('ab'.repeat(3))
//prints 'ababab'
Enter fullscreen mode Exit fullscreen mode

Destructuring array

let [a,b] =[3,7];
console.log(a);//3
console.log(b);//7

Enter fullscreen mode Exit fullscreen mode

Destucturing object

let obj ={
a:55,
b:44
};
let{a,b}= obj;
console.log(a);
//55
console.log(b);
//44
Enter fullscreen mode Exit fullscreen mode

Object property assignment

const a=2
const b=5
const obj={a,b}
//before es6:
//obj ={a:a,b:b}
console.log(obj)
//{a:2,b:5}
Enter fullscreen mode Exit fullscreen mode

Object.Assign()

const obj1 ={a:1}
const obj2 ={b:2}
const obj3 =Object.assign({},obj1,obj2)
console.log(obj3)
//{a:1 ,b:2}
Enter fullscreen mode Exit fullscreen mode

Promises with finally

promise
.then((result) => {...})
.catch((error) => {...})
.finally(() => { 
    //Logic independent of success/error 
})
/* The handeler is called when the promise is fulfilled or rejected.*/
Enter fullscreen mode Exit fullscreen mode

Top comments (10)

Collapse
 
cmgustin profile image
Chris Gustin

For the Template String example, the line with the template string needs to be wrapped in back ticks instead of single/double quotes or it won’t parse.

Collapse
 
jamesthomson profile image
James Thomson • Edited

The Object.Assign example has a similar issue. object needs to be Object or else it'll throw a reference error. Same with the promise example. 🤦‍♂️ ...In fact, there are a lot of examples here missing closing quotes or with improper casing which will throw errors.

Collapse
 
buddhadebchhetri profile image
Buddhadeb Chhetri

Thanks for the correction I will fixed that.😊

Thread Thread
 
cmgustin profile image
Chris Gustin

I think the multi-line string example needs back ticks too, and the example code string is showing for me as one line.

Thanks for editing those other examples.

Thread Thread
 
buddhadebchhetri profile image
Buddhadeb Chhetri

Thanks for the correction:)

Collapse
 
jamesthomson profile image
James Thomson

Unless you want it to output 'Hello ${name}'😉

Collapse
 
davidrojom profile image
David Rojo

Nice post!

There's a typo in spread operator first line

Collapse
 
buddhadebchhetri profile image
Buddhadeb Chhetri

Thanks bro .

Collapse
 
pankaj_singhr profile image
Pankaj Singh

Nice post

Collapse
 
mishabka profile image
Mishab Ka

Nce

Some comments have been hidden by the post's author - find out more