Pour typed ingredients into labeled vials, mind the wax seals of const, and learn the true chemistry of JavaScript coercion โ one bubbling cauldron at a time.
A traveling alchemist's notes on the true nature of ingredients, vials, and chambers.
let and constA vial poured with let is corked loosely โ you may re-pour fresh contents into it whenever you please. A vial sealed with const is closed with hardened wax the moment it's filled. Attempt to re-pour it and the seal cracks, the glass shatters โ this is JavaScript's real behavior: reassigning a const throws a TypeError: Assignment to constant variable.
Pour a Number essence and a String essence into the same cauldron, and JavaScript does not refuse โ it coerces. The number is dragged into text and the two are concatenated, producing a murky new brew rather than a clean sum. Two numbers, however, blend into a brighter, purer potion through true addition.
The Global Cauldron holds ingredients any brewer may reach. A Function Flask, once corked, holds its own ingredients privately โ sealed from the outside. A Block Vial, poured inside { curly braces }, is the most private of all, discarded the instant the brewing step ends. Pour into the wrong chamber and your ingredient simply cannot be reached from outside it.
Each potion in the ledger is a tiny working snippet. Pour the correct typed ingredients, in the correct chamber, respecting every seal, and the cauldron will bottle a glowing potion for your shelf. Pour wrongly, and the brew merely fizzes, smokes, or shatters glass โ a safe lesson, never a punishment.
Every potion in this chapter now glows upon your shelf. You have mastered variables, coercion, immutability, and scope โ the true chemistry of JavaScript.