Available types of variables in solidity

Zinzuvadiya Meet
2 min readMar 22, 2022

--

Solidity supports three types of variables.

  1. State Variable
  2. Local Variable
  3. Global Variable

Solidity is a statically typed language, which means that we have to be specified the state or local type variables during declaration. Each declared variable always have a default value based on its type. There is no concept of “undefined” or “null”.

State Variable

Variables which are mentioned and stored within the contract. In other words, values are permanently stored in a contract storage.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract VariablesInSol{
address ownerAddress; //State variable
constructor() {
ownerAddress=msg.sender; //using State variable
}
function getOwnerAddress() public view returns(address){
return ownerAddress; //return State variable
}
}

In this example, we can access ownerAddress variable at anywhere within the contract,

Local Variable

Local variables are that which use within the functions.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract VariablesInSol{
function getWalletBalance(address _address) public view returns(uint){
uint walletBalance = _address.balance; //Local variable
return walletBalance;
}
}

In this example, we can access walletBalance variable within browser, but we can not access it outer the function block.

Global Variable

There are spacial variables which exist in global workspace and provide information about the blockchain and transactions.

Below I mention some global variables. For batter understanding I discussed it into three different categories:

  • tx: it represents transactions.
  • msg: it represents messages.
  • block: it returns data about block.

For more variables I provide here I provide official solidity documentation link(click here).

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract VariablesInSol{
function globalVariables() public view returns(address, uint, uint){
address _address = msg.sender; //global variable
uint timestamp = block.timestamp; //global variable
uint blockNumber = block.number; //global variable
return (_address, timestamp, blockNumber);
}
}

On above example, I return three common global variables.

Below I added my gist file according to upper example for your easiness

Gist url for access full code (click here)

♥️️♥Thank you for reading this article. If you like it don’t forget to hit on 👏🏻 clap.

--

--

Zinzuvadiya Meet

Hey there! I’m a mobile app developer who passionate about creating websites & applications through flutter as well as provides firebase services to clients.