This section explains how to write the logical negation operators (not) in Solidity.
Syntax
The way to write the logical negation operator (not) in Solidity is to precede the boolean value with an exclamation mark (!) before the boolean value.
!boolean value
Sample Code
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Test {
function evenOrOdd(uint num) public pure returns(string memory) {
string memory message = "Even";
bool isEven = num % 2 == 0 ? true : false;
// isEven is not true
if (!isEven) {
message = "Odd";
}
return message;
}
}
Solidity has a unique syntax, but the logical negation operator is the same as in many other programming languages! The logical negation operator is a smart way to write code, so be sure to keep it in mind.