Solidity is a statically typed programming language, Each variable you use in solidity must declare of type. Like any programming language, It is used to store the value in a variable.

Variables in solidity are used to store the data in the blockchain as well as used in functions for processing.

This tutorial covers

  • Declare variable Syntax
  • Solidity variable examples

Solidity Variable Syntax

variables are declared with the type of variable and visibility or access modifier and variable name.

datatype  access-modifier variablename

Datatype is an inbuilt or custom user-defined data type such as boolean, or unit256.

access-modifier: Also called visibility and how and where the variable values are accessible. Possible values are

  • public
  • private
  • internal
  • external

variablename is a valid identifier in solid, It contains a combination of alphabets and numeric. The reserved keywords are not used.

The variables can be declared in Contracts or functions

Variable type initial default values

creating a variable does assign default initial values.

Based on data type, initial values are different as given.

These default values are assigned when variables are not assigned any value.

Data TypeDefault valueValue/Reference type
boolfalseValue type
stringempty string("")Value type
int/uint0Value type
fixed/ufixed types0.0Value type
address0x0000000000000000000000000000000000000000Value type
enumfirst element of an enum constantValue type
Internal functionIt returns an empty function, if it contains retur, It returns empty valuesValue type
external functionfunction returnsValue type
mappingblank empty mappingReference type
structreturns a struct with initial default valuesReference type
Fixed arrayAll the items are default valuesReference Type
Dynamic arrayEmpty Array([])Reference Type