Array spreading
let array1 = [1,2,3];
let array2 = [4,5,6];
let result = [...array1, ...array2];
console.log(result); // [1,2,3,4,5,6]
Object spreading
let obj1 = {a:1, b:2, c:3};
let obj2 = {d:4, e:5, f:6};
let result = {...obj1, ...obj2};
console.log(result); // {a:1, b:2, c:3, d:4, e:5, f:6}
Core functionalities of a Prototype-based language:
{...obj1, ...obj2};
[...array1, ...array2];
Object Destructuring
let { a, b, c } = { a:1, b:2, c:3 };
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
Object Destructuring
let [a,b,c] = [1,2,3];
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
async/await
async someAsyncCode() {
// save() is asynchronous (e.g Promise)
let animal = await db.save(createAnimal());
console.log("Sucessfully saved: " + animal);
// load() is asynchronous (e.g Promise)
let loaded = await db.load(animal.id);
console.log("Sucessfully loaded: " + loaded);
}
async functionName(someArgs) {
...
let result = await someAsyncFunction(someArgs2);
...
}
function padLeft(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
function padLeft(num, size) {
var s = "000000000" + num;
return s.substr(s.length - size);
}
by Stackoverflow et al.
"1".padStart(4, "0"); // 0001
"2".padEnd(4, "0"); // 2000
Check out the new String API !
// trim right
" abc ".replace(/^\s+/g, ''); // "abc "
// trim left
" abc ".replace(/\s+$/g, ''); // " abc"
// trim both
" abc ".replace(/^\s+/g, '').replace(/\s+$/g, ''); // "abc"
by Stackoverflow et al.
" abc ".trimLeft(); // "abc "
" abc ".trimRight(); // " abc"
" abc ".trim(); // "abc"
Check out the new String API !
// ⚠ ️Notice ...
let string = '❤️❤️❤heart'; // 👈🏼 emojis !
console.log(string == '\u2764\uFE0F\u2764\uFE0F\u2764heart');
console.log(string.length == 10); // true ;)
console.log('❤️' == '\u2764\uFE0F');
encoding gets slightly bit more complicated ;)
possibilities vs consequences
Reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid
For all nullish values, undefined is returned.
if (someObject) {
return someObject.someProperty;
}
if (someObject && someObject.someProperty) { ... }
return someObject?.someProperty;
someObject?.someFunction?.();
As with properties it will return undefined if the function wasn't found
let undefinedValue;
// current solution
const undefinedValue = undefinedValue || 'some default';
// unexpected results with falsy values like 0, false...
// with nullish coalescing
const nullValue = undefinedValue ?? 'some default';
// no other falsy values than "null" and "undefined" considered
Check out the latest APIs - they probably changed since you read them the last time !