The logical NOT operator in Swift is a prefix operator that negates or reverses the Boolean value of its operand. When the operand is
true, it returns
false, and when the operand is
false, it returns
! operator, unlike the
|| operators, only works on a single value.
Assume the variables below have Boolean values.
let x = false; let y = true;
When you add the
! operator to both values, you get the opposite Boolean:
print(!x); // Prints: true print(!y); // Prints: false
If an expression is enclosed in parentheses, the
! operator can also be used to negate the value of that expression. The logic within the parentheses is first executed, then negated in the following example.
print(!(true && true)); // Prints: false
true && true results in
!true results in
false, the result will be
Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.