Skip to content

Instantly share code, notes, and snippets.

View nivrith's full-sized avatar
🎸
Recreational Mathemusician

Nivrith nivrith

🎸
Recreational Mathemusician
View GitHub Profile
@nivrith
nivrith / Filter Falsey values out of an array.js
Created November 27, 2020 09:52
Filter Falsey values out of an array
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);
@nivrith
nivrith / Destructured object parameters are better than multiple function parameters.js
Last active November 27, 2020 09:51
Destructured object parameters are better than multiple function parameters
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});
@nivrith
nivrith / functions with multiple parameters are unreadable.js
Last active November 27, 2020 09:49
functions with multiple parameters are unreadable
function getImageURLById(imageId, asAttachment = false, asThumbnail = false, withCompression = false) {
...
}
// This is unreadable without checking function definition
getImageURLById(25, true, false, true);
@nivrith
nivrith / Use Set to filter Uni values in array.js
Created November 27, 2020 09:47
Use Set to filter Uni values in array
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]
@nivrith
nivrith / Trouble with default assignment with logical or.js
Last active November 27, 2020 09:46
Trouble with default assignment with logical or
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
@nivrith
nivrith / Default assignment using logical or.js
Created November 27, 2020 09:44
Default Assignment using Logical or
let length= 0;
let name= "";
let length = number || 25;
let name = name || "John";
@nivrith
nivrith / optional chaining.js
Created November 27, 2020 09:42
Optional Chaining
let petName;
if(person && person.pet && person.pet.name) {
petName = person.pet.name;
}
//Can be condensed to 
let petName = person?.pet?.name;
@nivrith
nivrith / Iterables in JavaScript.js
Created November 20, 2020 06:25
Iterables in JavaScript
const numbers = [1, 2, 3, 4];
for (const number of numbers) {
console.log(number)
}
// 1
// 2
// 3
// 4
@nivrith
nivrith / queue-7-of-7.ts
Created November 20, 2020 06:07
Make your own iterable
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);
@nivrith
nivrith / you can destructure iterables.ts
Created November 20, 2020 05:47
Destructuring Iterables
const [first, ...rest] = new Range(1, 10);
console.log(first); // 1
console.log(rese); // [2, 3, 4, 5, 6, 7, 8, 9, 10]