!), AND (
&&), and OR (
! (NOT). Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type.
This logical NOT operator (!), also called the bang operator, operates on an expression. It returns the opposite of the expression’s truthiness. For example if
a = true then
!a will return
false , else if
a = false then
!a will return
You can also convert an expression to a Boolean using
!!. As a shorter to convert any value into a boolean, we can just use the two NOT operators from above.
a = "this is truthy"
!a => //returns false
!!a => //returns true
! and turn to the right to see what we are asking to invert. The engine resolves the variable
"this is truthy. Then it sees the first
! and inverts the variable to return false. Right now seeing
!!a,we’re no longer see the variable but we’re looking at
!false which will return the opposite => true.
3. && AND
The logical AND operator (&&) takes two expressions.
expression1 && expression2
The return value of the
&& operator is always one of the two expressions. If the first expression is false, the AND operator returns the value of the first expression. If the first expression is truthy then the AND operator will return the second expression.
- *NOTE — if the first expression is false the
&&operator will return the first expression without ever checking the second expression.
- If both expressions are truthy then the engine will check the first expression and then follow with second and return the second expression.
The logical OR
|| also takes in two expressions. The return value of the
|| operator is always one of the two expressions. If the first expression is true the OR operator returns the value of the first expression. If the first expression is false then the OR operator will return the second expression.
- **NOTE — If the first expression is true, then the value is immediately returned and the second expression is never even evaluated
- If the first expression is false, then the OR operator returns whatever the second expression evaluates to