Solidity provides different scopes based on the place of variable declaration in a contract.

  • Global Scope(State) variable
  • Local Variable
  • Global Inbuilt Variable

Solidity State variable

variable declared in the contract in one of the scopes.

variable declared in the contract are called state variables

data is stored permanently in blockchain storage

pragma solidity ^0.6.0;
contract SolidityTest {
   uint stateData;      // State variable
   constructor() public {
      stateData = 40;   // Using State variable


  • These variables are updated or assigned initially or constructor or setter functions -State variables are stored in Blockchain data storage
  • It is an expensive gas operation
  • Blockchain storage is fixed not dynamic
  • Contract object Instance has state variables declared only in it

Local Variables

variables declared in functions are called local variables. The scope is limited to function and data of these variables are stored in Stack in EVM.

pragma solidity ^0.5.0;
contract VariablesContractTest {
   *State variables are declared inside a contract ie global scope
   uint age; // State variable
   constructor() public {
      age = 10;
   function add() public view returns(uint){
      // local variables are declared and accessed inside a function
      uint localVariable1 = 12; // local variable
      uint localVariable2 = 2; // local variable
      uint sum = localVariable1 + localVariable2; // local variable
      return sum; // access the local variable


  • These variables are local to the function and stored in a Stack in EVM
  • It is only accessible in the declared function and the variable is not available to access outside a function
  • It is an inexpensive gas operation and does not cost gas
  • These variables are referred to as state variables that stored in the storage

As of now, Learned global and local custom variables Next, learn Global predefined inbuilt scope variables in solidity

Global Inbuilt Variable

Solidity defines the Global scope of predefined variables and is accessible anywhere contract running in the blockchain.

It returns the block, transaction, and account information of the running blockchain node.

Global VariableReturn TypeDescription
block.difficultuintreturns block difficult
block.chainiduintreturns Current block Chain ID
block.gaslimituintreturns Current block Gas Limit
block.numberuintreturns Current block number
block.timestampuintreturns Current block timestamp in seconds from Unix epoch time(1970-01-1
msg.databytesIt returns caller message data
msg.senderaddressIt returns sender caller details
msg.sigbytes4Returns first four bytes of a caller message data
msg.valueuintReturns number wei sent
tx.gaspriceuintreturns gas price of current transaction
tx.originaddresscurrent transaction sender call chain details

All the above global inbuilt variables are used anywhere in a contract.

Here is an example code of how to use global variables in solidity contracts.

pragma solidity ^0.5.0;

contract GasCostCalculator{

   uint public gasCost;

   constructor() public {
      gasCost = tx.gasprice;