Exiting ES6 features for NodeJS

Setting defaults for function parameters

1
2
3
4
5
function toThePower(value, exponent = 2) {
// The rest of your code
}
toThePower(1, undefined) // exponent defaults to 2

Extracting Data from Arrays and Objects with Destructuring

TODO

Checking Array Values with Array#includes()

1
2
3
[1, 2].includes(1) // returns true
[1, 2].includes(4) // returns false

find/findIndex

1
2
3
4
let ages = [12, 19, 6, 4];
let firstAdult = ages.find(age => age >= 18); // 19
let firstAdultIndex = ages.findIndex(age => age >= 18); // 1

Allowing Extra Arguments in Functions

1
2
3
4
5
6
7
8
9
function concat(joiner, ...args) {
// args is an actual Array
return args.join(joiner)
}
concat(‘_’, 1, 2, 3) // returns ‘1_2_3’

Expanding Arrays with the Spread Operator

1
2
3
4
5
6
7
const arr1 = [1, 2]
const arr2 = [...arr1, 3, 4]
// arr2: [1, 2, 3, 4]
let numbers = [9, 4, 7, 1];
Math.min(...numbers); // 1

for … of

The for...of statement creates a loop iterating over iterable objects (including Array, Map, Set, String, TypedArray, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Syntax
for (variable of iterable) {
statement
}
let iterable = "boo";
for (let value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"