The delete operator can reset the value of the variable to their default value.
What is the difference between the constant and immutable keywords in Solidity and how do I use them?
| Feature | constant |
immutable |
|---|---|---|
| Assignment Time | Compile-Time | At Declaration(Compile-Time) or Constructor(Deployment-Time) |
| Flexibility | Less flexible; values must be known upfront | More flexible; values can be set during deployment |
| Use Cases | Fixed values like mathematical constants, fixed addresses | Dynamic values like deployer address, configuration parameters |
| Gas Efficiency | Slightly more efficient due to compile-time embedding | Efficient; stored in bytecode after deployment |
| Reassignment | Not allowed after initial assignment | Not allowed after initial assignment |
<aside> 💡
immutable Variables:
Assignment in Constructor:
immutable variable within the constructor body, using constructor parameters or deploy-time data (e.g., msg.sender, block.timestamp).
</aside>| Aspect | Compile Time | Deploy Time |
|---|---|---|
| When It Occurs | Before deploying the contract | During the actual deployment to the blockchain |
| What’s Set | Values known beforehand (constants) | Values determined at deployment (immutable variables) |
| Examples | constant variables like MAX_SUPPLY |
immutable variables like owner set in the constructor |
| Analogy | Writing and baking the recipe | Opening the bakery to the public |
constant for values that never change and are known upfront: Like the maximum number of tokens (MAX_SUPPLY).immutable for values that are set once when the contract is deployed: Like the owner’s address, which you might not know until deployment.