What are Logical Operators?

The Logical Operators

The logical operators are primarily used to control the program flow. The concept of logical operators is to allow programs to make decisions based on multiple terms and conditions. Each of the operands is considerably conditioning to determine the overall value. The logical operators have the symbol of two vertical lines (l l).

In classical programming, the logical operators meant to navigate the Boolean values only. If any of the arguments are true, then it returns true. If otherwise, it returns false. In JavaScript, the operators are somewhat trickier and more powerful.

The logical operators do the following:

  • It evaluates the operands starting from the left part to the right.
  • In each operand converts into Boolean. If the result is true, it stops and reverts to the original value.
  • All the operands resulted in false will revert to the last operand.

Values will return to its normal form without the conversion. In other words, the chain of logical operators returns the first true value or the last one if no true value found.

&& (AND)

The AND operator has the symbol of two ampersands (&&). In classical programming, AND returns true If both operands are truthy and false.

The AND (&&) operator does the following:

  • Evaluates the operands from the left part to the right
  • In each operand, convert it to Boolean. If the result is false, it stops and returns the original value of the operand.
  • All the operands evaluated truthy will return the last operand.

In other words, AND will return the first falsy value or the last value if found none. The rules are just similar to OR. The difference is that, AND will return the falsy value while OR returns the first truthy value. When all the values are truthy, then the last value will return.

! (NOT)

This logical operator has the symbol of an exclamation sign (!). The operator only accepts a single argument and only does the following:

  • Converts the operand to Boolean type: true/false returns the inverse value

Note that a double not (!!) means to convert an amount into Boolean type. The precedence of NOT (!) is the highest of all logical operators. It is always to execute first following by && or I I.

Logical expressions can only have the values of true and false. There are relational operators used to structure rational expressions. Logical operators are used to comparing two values having the same type. the table below will show the relational operators along with their meanings.

  Operator / Meaning 

  .eq.

  Equal to 

  .ne.

  Not equal to  

  .lt.

  Less than  

  .le.

  Less than or equal to  

  .gt.

  Greater than  

  .ge.

  Greater than or equal to

Some logical operators only used between complete logical expressions. The table below will show the logical operators along with the meanings:

  Operator / Meaning

  .not.

  Changes the value of the expression into the opposite value

  .and.

  TRUE only if both the logical expressions are TRUE

  .or.

  TRUE if either the logical expression is TRUE

  .xor.

  TRUS if only one expression is true

  .eqv.

  TRUE if the expression has the same truth value

  .neqv.

  TRUE if the expression does not have the same value

Conclusion:

Each of the logical operators has unique characteristics. All have a vital role in the programming field. It is essential to understand and distinguish each.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *