guess the output
one javascript snippet a day with a result that isn't what you'd expect. resets at midnight utc.
In what order do these log?
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");every puzzle12
the whole bank, with the answer and the reason why.
Why does [10, 9, 1].sort() return [1, 10, 9] in JavaScript?
console.log([10, 9, 1].sort());
Why is typeof null "object" in JavaScript?
console.log(typeof null, typeof NaN);
Why does setTimeout inside a var loop log 3 3 3?
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), …
Why does a resolved promise log before setTimeout(fn, 0)?
console.log("A"); setTimeout(() => console.log("B"), 0); Promis…
Why does spreading an object still mutate the nested object?
const a = { nested: { n: 1 } }; const b = { ...a }; b.nested.n …
Why is [] == false true but [] === false false?
console.log([] == false, [] === false);
Why does calling setCount twice only increment React state by one?
const [count, setCount] = useState(0); function onClick() { set…
Why does Array(2).fill([]) share the same array in every slot?
const grid = Array(2).fill([]); grid[0].push("x"); console.log(…
Why does ["1", "7", "11"].map(parseInt) return [1, NaN, 3]?
console.log(["1", "7", "11"].map(parseInt));
Why do o[1] and o["1"] set the same object key in JavaScript?
const o = {}; o[1] = "a"; o["1"] = "b"; console.log(Object.keys…
Why doesn't await work inside Array.prototype.forEach?
const ids = [1, 2, 3]; async function run() { ids.forEach(async…
Why is 0.1 + 0.2 === 0.3 false in JavaScript?
console.log(0.1 + 0.2 === 0.3);