
#JavaScript reminders for people who are keep losing the details. Short code, a plain explanation, one thing worth remembering everyday. @remindmepython
Joined March 2025
- Tweets617
- Following1
- Followers89
- Likes3.6K
findLast() searches from the end 🔎
const list = [1, 4, 7, 10];
[...list].findLast(n => n < 8); // 7
[...list].findLastIndex(n => n > 99); // -1
Like find(), but scans backward and returns the last match. No match: undefined, or -1 for the index version.
1
7
Object.groupBy() buckets items by key 🗃️
const { groupBy } = Object;
const nums = [1, 2, 3, 4, 5];
groupBy(nums, n => (n % 2 ? "odd" : "even"));
// { odd: [1, 3, 5], even: [2, 4] }
The callback returns each item's key. The result has a null prototype.
13
isWellFormed() spots broken surrogates 🧩
const s = "hi\ud83d";
s.isWellFormed(); // false
s.toWellFormed(); // "hi\ufffd"
"hi👋".isWellFormed(); // true
A lone surrogate isn't valid Unicode. toWellFormed() swaps it for the U+FFFD replacement character.
15
Array.from() takes a map function 🏗️
const { from } = Array;
from({ length: 3 }, (_, i) => i * 2);
// [0, 2, 4]
Any object with a length works as array-like. The second argument maps each item as it's built, so no extra .map() pass or intermediate array is needed.
18
charAt() and [] differ out of range 🔤
const s = "abc";
s.charAt(9); // ""
s[9]; // undefined
charAt() returns an empty string for an out-of-range index; bracket access returns undefined. Check against the right one.
21
flatMap() maps and flattens one level 🪄
const nums = [1, 2, 3, 4];
nums.flatMap(n => (n % 2 ? [] : [n, n]));
// [2, 2, 4, 4]
Return [] to drop an item, or several items to expand it. Only one level gets flattened.
23
delete leaves a hole in arrays 🕳️
const a = [1, 2, 3];
delete a[1];
console.log(a); // [1, <1 empty item>, 3]
console.log(a.length); // 3
delete removes the property, not the slot, so length never shrinks. Use splice(1, 1) to really remove it.
14
fn.length ignores defaults 📏
const a = (x, y) => {};
const b = (x, y = 1) => {};
const c = (x, ...rest) => {};
console.log(a.length, b.length, c.length);
// 2 1 1
length counts params before the first default or rest. Handy to know when a library inspects your callback.
toFixed() returns a string 🎯
(1.005).toFixed(2); // "1.00"
(2.5).toFixed(0); // "3"
typeof (1).toFixed(1); // "string"
It rounds the binary value, not the decimal you typed, so 1.005 is really slightly below. Wrap in Number() if you need a number back.
15
Integer-like keys jump the queue 🔢
const { keys } = Object;
const o = { b: 1, 2: "x", a: 2, 1: "y" };
keys(o); // ["1", "2", "b", "a"]
Integer-like keys come first, ascending; string keys keep insertion order. Don't rely on insertion order for numeric IDs.
21
Tagged raw keeps backslashes literal 🧵
const { raw } = String;
raw`C:\new\table`; // C:\new\table
"C:\new\table"; // newline + tab inside
The raw tag skips escape processing, so paths and regex sources stay as typed.
1
35
parseFloat reads until it can't 📖
parseFloat("3.14abc"); // 3.14
parseFloat("abc3"); // NaN
Number("3.14abc"); // NaN
parseFloat keeps the valid prefix and ignores the rest. Number is strict: any bad character gives NaN.
18
Math.max() with no args is -Infinity ♾️
const { max, min } = Math;
max(); // -Infinity
min(); // Infinity
max(...[]); // -Infinity
Both start from the identity value, so an empty list never gives 0 or NaN. Guard empty arrays before spreading.
18
includes() finds NaN, indexOf() doesn't 🔍
const list = [NaN];
list.includes(NaN); // true
list.indexOf(NaN); // -1
includes() uses SameValueZero, so NaN matches itself. indexOf() uses ===, and NaN !== NaN.
18
Past 2**53, integers lose precision 🔢
const { MAX_SAFE_INTEGER: max } = Number;
max + 1 === max + 2; // true
9007199254740993n; // BigInt stays exact
Numbers are 64-bit floats, so above 2**53 - 1 neighbors collapse. Use BigInt for big ids.
16
typeof null is "object" 🕳️
console.log(typeof null);
console.log(typeof undefined);
console.log(null === null);
// "object"
// "undefined"
// true
A decades-old bug, kept for compatibility. To check for null, compare directly with === null instead of using typeof.
24
fill() reuses one object 🧩
const rows = Array(2).fill([]);
rows[0].push("x");
// rows is [["x"], ["x"]]
fill() puts the SAME reference in every slot. For separate arrays use Array.from({ length: 2 }, () => []).
10
slice() can cut an emoji in half ✂
const s = "hi👋";
s.slice(0, 3); // "hi\ud83d" (broken)
[...s].slice(0, 3).join(""); // "hi👋"
Indexes count UTF-16 units, and 👋 takes two. Spread into characters first when you trim text for a preview.
18
Number(null) is 0, Number(undefined) is NaN 🧮
Number(null) // 0
Number(undefined) // NaN
Number("") // 0
Number(" 12 ") // 12
null becomes 0, undefined has no number, and an empty or blank string also becomes 0.
14