Logical NOT (!) Expression

Summary

Negates an expression.

Syntax

!expression

Parameters

expression
Any legal expression.

Description

The logical NOT (!) operator negates an expression. Therefore, if an expression evaluates to true, this operator applied to the expression will return false; if an expression evaluates to false, this operator applied to the expression will return true.

1
2
3
4
!true// false
!false; // true
!( 5 > 3 ); // false
!( 5 == 1); // true

Inequality (such as comparing if x does not equal y) can also be checked by using the inequality (!=) comparison and strict inequality (!==) comparison operators.

Usage in Conditional Statements

The logical NOT operator is commonly used in conditional statements, such as if statements, to test if conditions are true or false. For example:

1
2
3
4
5
6
import System;
 
bool x = false;
if (!x) {
    Console.log("'x' is false");
}

See Also

Share

HTML | BBCode | Direct Link