Show Menu
Cheatography

ES6 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Let and Const

let and const
Hoisting problem
let -- allows block scoping and hoisting problem in ES5 is solved in ES6.Va­riables declared with the var keyword can not have Block Scope. Variables declared inside a block {} can be accessed from outside the block.
When we use a undeclared variable with var keyword in ES5 we get undefined variable name error. This is the example for hositing problem.
const -- It does NOT define a constant value. It defines a constant reference to a value. Because of this, we cannot change constant primitive values, but we can change the properties of constant objects.
Whereas when we use let keyword , hoisting problem is solved in ES6. We get the Error as Reference Error <va­riable name> not defined.

This keyword

The JavaScript this keyword refers to the object it belongs to. It has different values depending on where it is used: In a method, this refers to the owner object. Alone, this refers to the global object
The 4 rules of finding out the value of this keyword
Rule 1 : When the keyword this is not inside the declared object then it refers to the global object
Rule 2 : When the keyword this is inside the declared object , then it refers to the closest parent object
Rule 3 : whenever the context of the object changes , we use call , apply and bind to set the value of this explic­itly.
Rule 4 : Whenever we create a object using new keyword inside the function defini­tion, the this keyword refers to the new object that is being created

Arrow Functions

Arrow function or fat arrow function -- shorter version of syntax when compared to the normal function
We cannot manipulate the value of this keyword inside the arrow function when we use call,apply or bind
We do not have access to the prototype field when we declare the function using fat arrow symbol

Default function parameters

when we set up default function parameters we get access to the functions and the variables in the context
data=(­pri­ce,­cos­t=0.07­)=>{ consol­e.l­og(­pri­ce*­cost) } data(5.00)
 

Rest and spread operator

Rest
Spread
It allows to convert the no of parameters into an array
It allows to convert the array into an parameters
It is denoted by "..."­ in the function definition or function expression
It is also denoted by the "..."­ , but used to destru­cture the array
a = (...da­ta)­=>{ consol­e.l­og(­data) } a(2,3,­3,3,3)
(5) [2, 3, 3, 3, 3]
a(2,3,­3,3­,3,­)(5­) [2, 3, 3, 3, 3]
Spread operator can split the string into char
The rest parameters must be at the end
a = [...'acd'] (3) ["a­", "­c", "­d"]

Object Literal

It is shorthand for initia­lizing the object properties and also method

Prototype

All JavaScript objects inherit properties and methods from a prototype.
When we create the constr­uctor function , prototype property is created for that constr­uctor function
The only inconv­enience of using prototypes is that there is no easy way to create private methods or variables.

for of loop

//for of loop is used in iterable

var a = [1,2,2,2,2];

for ( let i of a) {
console.log(i);
}

Octal and binary Literals

var a = 0O12; //octal literals either O or o is allowed
console.log(a)//12
var f = 0b111;
console.log(f);

Template literals

It can create the multiline strings

new.target

The new.target property lets you detect whether a function or constr­uctor was called using the new operator. In constr­uctors and functions instan­tiated with the new operator, new.target returns a reference to the constr­uctor or function. In normal function calls, new.target is undefined.
Ref -- https:­//d­eve­lop­er.m­oz­ill­a.o­rg/­en-­US/­doc­s/W­eb/­Jav­aSc­rip­t/R­efe­ren­ce/­Ope­rat­ors­/ne­w.t­arget

Example

class A{
constr­uct­or(){
this.data = 55;
consol­e.l­og(­"­Inside the base")
consol­e.l­og(­new.ta­rge­t.d­umm())
}
}

class B extends A{
constr­uct­or(){
super()
consol­e.l­og(­new.ta­rget)
consol­e.l­og(­typeof B)
this.data = 66;
consol­e.l­og(­thi­s.data)
}
static dumm(){
return 57;
}
}