Search⌘ K
AI Features

Variables

Explore the different types of variables in Solidity including state, local, and global variables. Understand their scope, storage behavior, and how to declare and use them effectively within Ethereum smart contracts. This lesson also covers important guidelines for naming variables and introduces key global variables related to blockchain context, preparing you for managing contract data efficiently.

In Solidity, there are three different types of variables:

  • State variables: These are kept in a contract storage facility permanently.

  • Local variables: These retain their values while a function is running.

  • Global variables: These are used to access information about the blockchain.

The state or local variable type must be declared because Solidity is a statically typed language: based on the variable type, every defined variable has a default value.

Guidelines for variable naming

  • Avoid using reserved keywords: Variable names shouldn’t contain any reserved keywords. For example, we can’t use keywords like contract or function as variable names.

  • Start with a letter or underscore: Variable names must begin with a letter or an ...