Skip to content

Instantly share code, notes, and snippets.

@alexZajac
Created May 14, 2020 22:36
Show Gist options
  • Select an option

  • Save alexZajac/53e34f255a50c040d815a6c1b881215c to your computer and use it in GitHub Desktop.

Select an option

Save alexZajac/53e34f255a50c040d815a6c1b881215c to your computer and use it in GitHub Desktop.
A small gist following the artcile on "hidden" useful JS features
// 1 - The + operator
const plus = () => {
// get timestamp in a shorthand syntax
console.log(+new Date());
// boolean to integer conversion
console.log(+true);
console.log(+false);
// scientific notation conversion
console.log(+"1e8");
console.log(+"1.6e-2");
// my favorite, random generation
const random = {
valueOf: () => Math.floor(Math.random() * 100),
};
console.log(+random);
console.log(+random);
console.log(+random);
};
// 2- The debugger statement
// run in the browser or with --inspect for node
const debug = () => {
const bubbleSort = (arr) => {
const length = arr.length;
for (let i = 0; i < length; i++) {
for (let j = 0; j < length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
debugger;
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
};
console.log(bubbleSort([8, 2, 19, 8, 4, 5, 2]));
// 2, 2, 4, 5, 8, 8, 19
};
// 3- The comma operator
const comma = () => {
let money = 10;
const hasStudied = false;
const relax = () => console.log("relax");
const study = () => console.log("study");
hasStudied ? (money++, relax()) : ((money /= 2), study());
console.log(money); // study 5
// multiple variables in loops
for (let i = 1, j = 2; i + j < 10; i++, j++) {
console.log(i, j);
}
// 1 2; 2 3; 3 4; 4 5;
// for debugging purposes
const coins = [
{ value: 4, currency: "EUR" },
{ value: 1, currency: "USD" },
{ value: 5, currency: "BTC" },
];
const countOddCoins = (coins) => {
let oddCoins = 0;
coins.forEach(({ value, currency }) => {
if (value % 2 != 0) {
oddCoins++, console.log(`${oddCoins} with ${currency}`);
// 1 with USD
// 2 with BTC
}
});
return oddCoins;
};
console.log(countOddCoins(coins)); // 2
};
// 4- The Set() Object
const setDemo = () => {
// remove duplicates from Array
const arr = [1, 1, 7, 5, 6, 6, 6, 8, 7];
// old way
let noDup = arr.filter((a, b) => arr.indexOf(a) === b);
// new way
noDup = [...new Set(arr)];
console.log(noDup);
// 1 7 5 6 8
// traditional set operation
let a = new Set("hello world!");
// 'h''e''l''o'' ''w''r''d''!'
let b = new Set("medium is cool!");
// 'm''e''d''i''u'' ''s''c''o''l''!'
// intersection
const intersection = (a, b) => {
let intersection = new Set();
for (let elem of b) {
if (a.has(elem)) {
intersection.add(elem);
}
}
return intersection;
}
console.log(intersection(a, b));
// 'e''d'' ''o''l''!'
};
// 5- Web APIs (to run in the browser)
const webapi = () => {
// Performance API
const start = performance.now();
const fib = n => {
if(n <= 1) return n;
return fib(n-1) + fib(n-2);
}
fib(15);
const end = performance.now();
console.log(end - start);
// 0.1699999994598329 (on my machine)
// Navigator API
navigator.geolocation.getCurrentPosition(pos => {
const { coords: { latitude, longitude } } = pos;
console.log(latitude, longitude);
// 48.9080891 2.2699974
});
}
// Uncomment to run part
// plus()
// debug()
// comma()
// setDemo();
// webapi()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment