This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written as of ES2019 | |
await Promise.resolve(console.log('🎉')); // → SyntaxError: await is only valid in async function | |
(async function() { | |
await Promise.resolve(console.log('🎉')); // → 🎉 | |
}()); | |
// Top level await | |
await Promise.resolve(console.log('🎉')); // → 🎉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const number1 = 10; | |
const number2 = 0; | |
// Written as of ES2019 | |
const score1 = number1 || 1; | |
console.log(score1); // 10 | |
const score2 = number2 || 1; | |
console.log(score2); // 1 | |
// Using nullish coalescing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User { | |
#salary = 1000; | |
} | |
const newUser = new User(); | |
console.log(newUser.salary) // undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written as of ES2019 | |
if (onError) { // Testing if onError really exists | |
onError(); | |
} | |
// Using optional chaining with function calls | |
onError?.(); // no exception if onError is undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ | |
name: 'Nguyễn Hưng Khánh', | |
girl_friend: { | |
name: 'Nguyễn Thị Khánh', | |
address: 'Hà Nội' | |
} | |
}, | |
{ | |
name: 'Nguyễn Văn Khánh' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(users[1]?.girl_friend?.name) | |
// output: undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// users[0] | |
console.log(users[0].girl_friend.name) | |
// users[1] | |
if (users[1].girl_friend) { | |
console.log(users[1].girl_friend.name) | |
} else { | |
console.log('Lêu lêu. Thằng này ê sắc ế') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ | |
id: 1, | |
name: 'Nguyễn Hưng Khánh', | |
girl_friend: { | |
name: 'Nguyễn Thị Khánh', | |
address: 'Hà Nội' | |
} | |
}, | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getMoneySpent(keyboards, drives, b) { | |
/* | |
* Write your code here. | |
*/ | |
let max = -1; | |
for(let i = 0; i < keyboards.length; i++) { | |
const keyboardPrice = keyboards[i]; | |
for(let j = 0; j < drives.length; j++) { | |
const drivePrice = drives[j]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pageCount(n, p) { | |
/* | |
* Write your code here. | |
*/ | |
const pageFromStart = Math.floor(p/2); | |
const pageFromEnd = Math.ceil(((n % 2 !== 0 ? n - 1 : n) - p)/2); | |
return Math.min(pageFromStart, pageFromEnd); | |
} |