{

Learn Typescript Logical operators & Examples


Typescript Logical operators

Boolean operators

Logical operators are basic operators in a programming language.

They compare A Boolean expressions and return A Boolean values. Boolean expressions are multiple conditions where each condition returns true/false. Then these conditions can be combined using this logical operator, the output result is A Boolean.

The syntax and usage are the same in javascript too.

This operator is also called the boolean operator in typescript.

Here is a boolean operator syntax.

Operand operator operand

The operand is an expression that evaluates to true/false. Three logical operators in typescript are And, OR, and Not.

Logical OR Operator

The symbol for this operator is two vertical lines(||).

The operand is a conditional expression which boolean.

We can combine multiple operands, precedence is from left to right.

Here are the possible conditional values for OR operator examples

console.log(true || true )   // returns true    
console.log( false || true )   // returns true    
console.log( true || false)  // returns true    
console.log('string1' || 'string2' )  // returns "string1"    
console.log( false || 'string3')   // returns "string3"   
console.log('string4' || false )  // returns "string4"    
console.log(''  || false )  // returns false    
console.log( false || '' )    // returns ""  

if one of the operators is true, then output returns true or else returns false.
All the expressions return true except for both using the false condition.

Important notes

  • The operator always calculate from left to right
  • If the first operand is true and the remaining operand doe not evaluated, the operator returns true.
  • if all operand evaluates it means subsequent operands from the left are false.

Logical And operator

And the operator symbol is double ampersands(&&).
This operator returns true if all the operands are evaluated as true. else return false.

console.log(true && true )   // returns true    
console.log( false && true )   // returns false  
console.log( true && false)  // returns false  
console.log('string1' && 'string2' )  // returns "string2"
console.log( false && 'string3')   // returns "false"  
console.log('string4' && false )  // returns "false"  
console.log(''  && false )  // returns ""  
console.log( false && '' )    // returns false  

Important notes

  • Always evaluates from left to right
  • if the first operand evaluates as false, returns false and other operations are not evaluated.
  • if all expressions evaluate, it returns the last operand evaluated value as output

Logical Not Operator

Not the operator symbol is an exclamation mark !. This operator returns the inverse of the operand value.

console.log(!true)   // returns false  
console.log(!false)   // returns true

Conclusion

In this tutorial, Learned a Logical operator with examples. And also expressions are evaluated from left to right for these operators.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.