The type of null is not null as expected. It's object.
// www.justjavascript.com
typeof null === 'object'; // true
const arr = [1, 2, 3];
return [0, ...arr]; // [0, 1, 2, 3]
const arr = [1, 2, 3];
arr.unshift(0); // returns new length: 4
return arr; // [0, 1, 2, 3]
const arr = [1, 2, 3];
return [...arr, 4]; // [1, 2, 3, 4]
const arr = [1, 2, 3];
arr.push(4); // returns new length: 4
return arr; // [1, 2, 3, 4]
// https://stackoverflow.com/a/1026087/1013
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// https://stackoverflow.com/a/5915122/1013
function rand(items) {
return items[Math.floor(Math.random() * items.length)];
}
There are only 8 falsy values. The others are truthy.
// https://developer.mozilla.org/en-US/docs/Glossary/Falsy
function f() {
// prettier-ignore
return [
Boolean(false),
Boolean(''),
Boolean(``),
Boolean(""),
Boolean(0),
Boolean(0n),
Boolean(-0),
Boolean(-0n),
Boolean(NaN),
Boolean(null),
Boolean(undefined)]
}
f(); // returns array of `false`s
// https://developer.mozilla.org/en-US/docs/Glossary/Falsy
function f() {
return [Boolean(new Boolean(false)), Boolean('false'), Boolean({}), Boolean([]), Boolean(-1)];
}
f(); // [true, true, true, true, true]
Double bang is like not not something. Todo: check if it's exactly the same.
// It's basically !(!0) that is converted to !(true) which in turn becomes false
!!0; // false
// https://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript/1758912#1758912
function f() {
//prettier-ignore
var a1 = a2 = 1 // this is like var a1 = (a2 = 1)
//prettier-ignore
var b1 = 1, b2 = 1
return [window.a1, window.a2, window.b1, window.b2];
}
f(); // [undefined, 1, undefined, undefined]
// 2020-5-8
// bigint at 70% https://caniuse.com/#feat=bigint
// symbol 93% https://caniuse.com/#feat=mdn-javascript_builtins_symbol
typeof undefined; // "undefined"
typeof null; // "object"
typeof true; // "boolean"
typeof 1; // "number"
typeof 1.1; // "number"
typeof 2n; // "bigint"
typeof 'some text'; // "string
typeof Symbol('some value'); // "symbol"
typeof function g() {}; // "function"
typeof {}; // "object"
typeof []; // "object"
typeof new Date(); // "object"
typeof Boolean // "function"
typeof typeof 10; // "string" (typeof returns string "number")
// What the heck is the event loop https://www.youtube.com/watch?v=8aGhZQkoFbQ
// Jaffa Cake Asia: https://www.youtube.com/watch?v=cCOL7MC4Pl0
// Sync BEFORE promises (microtask) BEFORE settimeout (macrotask)
const result = [];
function f() {
setTimeout(() => {
result.push('settimeout');
}, 0);
Promise.resolve().then(() => result.push('promise'));
Promise.resolve().then(result.push('this is not a function, so it executes sync'));
result.push('sync');
}
f();
setTimeout(() => {
console.log(result);
}, 1);
// https://javascript.info/bigint
2n > 1; // true
1n > 1; // false
1n == 1; // true
1n === 1; // false
1n < 1; // false
// ASI https://stackoverflow.com/a/2846298/1013
function f() {
return {
a: 'hello',
};
}
function g() {
return;
{
a: 'hello';
}
}
f(); // {a: "hello"}
g(); // undefined
//
function f() {
return 2 / 0 === Infinity;
}
f(); // true
Number.isNaN(0 / 0); // true
Number.isNaN('hey' / 0); // true
Number.isNaN('hey'); // false
// https://0.30000000000000004.com/
// For some reason JS is famous for this, but there are many languages
// that do the same.
// A way to think of it is it's like 1/3 is 0.3333(3) on base10.
// In base2 this thing happens for 0.1 + 0.2.
0.1 + 0.2 == 0.3; // false
0.1 + 0.2 === 0.3; // false
0.1 + 0.2; // 0.30000000000000004
// The same word if written back and forth.
// https://www.toptal.com/javascript/interview-questions
function f(str) {
return str === str.split('').reverse().join('');
}
f('aba'); // true
f('aab'); // false
// inspiration https://twitter.com/donavon/status/1308468196559773696?s=12
useEffect(() => {
const timerID = setTimeout(() => {
// do stuff
}, 2000);
return clearTimeout(timerID);
}, []);
mdjs
and mdjsf
to add snippets on vscode. Don't forget the ctrl + space!