> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/gas-opt/constant-and-immutable.md).

# constant and immutable

* Using the `constant` and the `immutable` keywords for variables that do not change helps to save on `gas` used.&#x20;
* The reason been that `constant` and `immutable` variables **do not occupy a storage slot** when compiled. They are saved inside the contract byte code.&#x20;

```solidity
//SPDX-License-Identifier:MITs
pragma solidity ^0.8.3;

interface IERC20 {    
function transfer(address to, uint256 value) external returns (bool);    
function approve(address spender, uint256 value) external returns (bool);    
function transferFrom(address from, address to, uint256 value) external returns (bool);   
//rest of the interface code
}

//Gas used: 187302
contract Expensive {

   IERC20 public token;
   uint256 public timeStamp = 566777;

   constructor(address token_address) {
       token = IERC20();
   }
}

//Gas used: 142890
contract GasSaver {
   IERC20 public immutable i_token;
   uint256 public constant TIMESTAMP = 566777;

   constructor(address token_address) {
       i_token = IERC20(token_address);
   }
}
```

* <https://blog.soliditylang.org/2020/05/13/immutable-keyword/>

## Summary

```solidity
    IERC20 public immutable token; 
```

* For constant variables, the value has to be fixed at compile-time,&#x20;
* For immutable, it can still be assigned at construction time.
* `immutable` is one of the easiest gas optimizations that can be made, and at the same time very effective.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/gas-opt/constant-and-immutable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
