Solia saysconstant is a compile-time literal embedded directly in bytecode (zero read cost); immutable is written once in the constructor and baked into runtime bytecode (3 gas to read, no SLOAD).
Both constant and immutable live outside storage, but they differ in when their value is fixed. constant must be a compile-time expression and is inlined wherever it appears — there is no opcode at runtime to "read" it; the value just sits where it would have been used. immutable is set once in the constructor and patched into the runtime bytecode at deploy time; reads use a PUSH-like operation at 3 gas.
Compared to a normal state variable (SLOAD ≈ 2,100 cold), immutable is essentially free and constant is literally free. Use them for fee rates, owner addresses set at deploy, version numbers, etc. The demo compares per-read gas across a million reads.
Power-ups you unlock
constant: compile-time literal, inlined at every use site
immutable: set in constructor, baked into runtime bytecode
constant read cost: 0 (no opcode)
immutable read cost: 3 gas (PUSH-like)
state-variable SLOAD: 2100 cold, 100 warm
The Reentrancy Reaper attacks — common mistakes
Trying to set immutable outside the constructor
Using constant for a value not known at compile time
Storing as a normal state variable what could be immutable
Confusing constant (compile-time) with view (read-only function)
Boss battleCompare the gas cost of 1,000,000 reads of a value held as constant, immutable, or a storage variable.