Solidity Booleans

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

In Solidity, a boolean is a data type that holds a value consisting of true or false. This can be represented with a 1 (for true), or 0 (for false), and the default value is false. Here is an example of a contract that uses a boolean (which is denoted with the keyword “bool”):

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract booleanExample{
    
    bool public lightSwitch;

    function getSwitch() public view returns(bool){
        return lightSwitch;
    }

    function changeSwitch() public {
        lightSwitch = !lightSwitch;
    }
} 

In the code above, we create a boolean variable named “lightSwitch” and create 2 functions. The first function is a “getter” function that simply returns the value of “lightSwitch”. The second functions flips the position of the boolean, meaning it will turn it true if it is false, or false if it is already true.

Booleans are “value type” variables in Solidity, which means when you set another variable equal to a previously-declared boolean, it will make a copy of that variable (instead of always referencing another variable).

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.