LOGBOOK

HELP

Quiz Entry - updated: 2026.07.30

What do constant and immutable do, and why do they save gas?

Both mark a variable that is set once and never changes. constant is for values fixed at compile time; immutable is for values set once in the constructor. They save gas because the value is stored in the contract's bytecode instead of a storage slot.

uint256 public constant MINIMUM_USD = 50 * 1e18;   // known at compile time
address public immutable i_owner;                   // set once in constructor

constructor() { i_owner = msg.sender; }

Reading and storing regular state variables uses (relatively expensive) storage slots. When a variable is written exactly once and never again, Solidity can bake it into the bytecode, which is cheaper to read. Use:

  • constant — assigned right where it's declared, from a compile-time value (naming convention: ALL_CAPS).
  • immutable — not known at compile time but assigned once in the constructor (convention: i_ prefix).

Trying to reassign either after its one assignment is a compile error. It's a common early gas optimization.

Go deeper:

From Quiz: IOTHACK / Smart Contracts & the Remix IDE | Updated: Jul 30, 2026