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 array = [1, 0 , {foo: 'bar'}, undefined, null]; | |
const truthyArray = array.filter(Boolean); | |
//or we can use the double bang !! to do the same thing | |
const truthyArray = array.filter(value => !!value); | |
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 getImageURLById({imageId, asAttachment = false, asThumbnail = false, withCompression = false}) { | |
... | |
} | |
// This is more readable and doesn't require checking the function definition | |
getImageURLById({imageId: 25, asAttachment: true, withCompression: true}); | |
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 getImageURLById(imageId, asAttachment = false, asThumbnail = false, withCompression = false) { | |
... | |
} | |
// This is unreadable without checking function definition | |
getImageURLById(25, true, false, true); | |
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 array = [1, 2, 2, 3,3, 4, 5, 5, 3, 6, 9] | |
const uniqueArray = [...new Set(array)]; | |
console.log(uniqueArray); //[1, 4, 6, 9] |
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(length); // 25 and not 0 | |
console.log(message); // "John" and not "" | |
let length = 0; | |
console.log(length || 99); // 99 | |
console.log(length ?? 99); // 0 |
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
let length= 0; | |
let name= ""; | |
let length = number || 25; | |
let name = name || "John"; |
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
let petName; | |
if(person && person.pet && person.pet.name) { | |
petName = person.pet.name; | |
} | |
//Can be condensed to | |
let petName = person?.pet?.name; |
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 numbers = [1, 2, 3, 4]; | |
for (const number of numbers) { | |
console.log(number) | |
} | |
// 1 | |
// 2 | |
// 3 | |
// 4 |
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 queue = new Queue(10); | |
queue.add(1); | |
queue.add(2); | |
queue.add(3); | |
queue.add(4); | |
for(let item of queue) { | |
console.log(item); |
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 [first, ...rest] = new Range(1, 10); | |
console.log(first); // 1 | |
console.log(rese); // [2, 3, 4, 5, 6, 7, 8, 9, 10] |