Table of Contents
Null in Solidity overview
We don’t have the concept of undefined or null in Solidity. Instead, we have a default or zero concept. By default, each value, variable, or object gets a slot in memory once it is created, filled by the default value, for example, zero.
Different programming languages support the concept of undefined
, null
, Null
, NaN
, nil
, None
, etc, for example, Python, C++, Javascript, and Go, but Solidity doesn’t.
Let’s explore the default values of different types in Solidity.
Non-dynamic sized types
Non-dynamic type variables are the static type variables. Meaning we cannot change the size of the variables. They have fixed sizes that are not changeable. Static-type variables have straightforward default values.
int
oruint256
have a default value of0
bool
have a default value offalse
address
type has a default value of zero address that is0x0000000000000000000000000000000000000000
struct
type has a default value of all its data members’ default types coupled in a tuple.
Dynamic sized types
Dynamic-sized type variables are the ones that can change their sizes. The sizes of these variables depend on the data they’re assigned. For example, an array can be of length 10 or length 1000.
- The
string
type has an empty string default value. - The array’s default value is an empty array.
- The
byte
default value is empty or nobyte
.
🔥 Check this course out: Create Your Own Ethereum Token in Just 30 Mins
Null in Solidity conclusion
Solidity has this weird feature where it doesn’t support undefined values. In some cases, having the undefined value is beneficial, while in others it is not. In either case, we’ll have to learn to cater to this problem, learn the default values of each type, and then work with them accordingly.
Try it out, ask any question, and let us know how it went by tagging Metaschool on Social Media.
Follow us on –
🔮Twitter – https://twitter.com/0xmetaschool
🔗LinkedIn – https://www.linkedin.com/company/0xmetaschool/