ES6 methods

I would like to learn more ES6 features, and therefore I will post my findings here. It’s just for fun and for my self to lookup.

Quick into

Let it be const

oh. The let and the const declaration of variables. Most likely 94 % of all js declarations uses the classic

1
var example = "this is a string";

This is all fine and good, but sometimes this gets confusing when dealing with scopes and duplicate declarations with the same name. For this purpose the word LET, comes to the rescue. look at the following pieces of code:

1
2
3
4
5
6
7
8
var Animal = "Panda";

while(true){
var Animal = "Rhino";
console.log(Animal); // -> Rhino
}

console.log(Animal); // -> Rhino

and

1
2
3
4
5
6
7
8
let Animal = "Panda";

while(true){
let Animal = "Rhino";
console.log(Animal); // -> Rhino
}

console.log(Animal); // -> Panda

This is due to the scoping of “let”, which will only be in scope of the next bracket. In the case of “var”, javascript uses something called Hoisting, which in short means that the code gets transformed into something like this:

1
2
3
4
5
6
7
8
9
10
var Animal;

Animal = "Panda";

while(true){
Animal = "Rhino";
console.log(Animal); // -> Rhino
}

console.log(Animal); // -> Rhino

Decunstruction

This is awesome for handling optional values to a method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// default values in the method signature and descrructuring
function deconstruc({
size,
price,
address = "unknown"
})
{

console.log('your house is: ' + size +' squarefeet, costs: ' + price +' and the address is ' + address);
}

var house = {
'size': 123,
'land': 2234,
'address': 'Fifth avenue 2',
'price': 1250000
};
var house2 = {
'size': 123,
'land': 2234,
'price': 1250000

};
deconstruc(house);
deconstruc(house2);