TypeScript Logical Operators

TypeScript logical operators are similar to what we have learned in JavaScript logical operators. These operators help in comparing boolean expressions and producing a single boolean value as result. 1. Logical Operators Operator Description Logical AND operator – && If both operands (or expressions) are true, then result …

angular-typescript

TypeScript logical operators are similar to what we have learned in JavaScript logical operators. These operators help in comparing boolean expressions and producing a single boolean value as result.

1. Logical Operators

Operator Description
Logical AND operator – && If both operands (or expressions) are true, then result will be true, else false.

let firstVar = true;
let secondVar = false;
 
console.log( firstVar && secondVar ); //false
console.log( firstVar && true );    //true
console.log( firstVar && 10 );      //10 which is also 'true'
console.log( firstVar && '10' );    //'10'  which is also 'true'

Read More: Truthy and Falsy

Logical OR operator – || If any of the two operands (or expressions) are true, then result will be true, else false.

let firstVar = true;
let secondVar = false;
 
console.log( firstVar || secondVar ); //true
console.log( firstVar || true );    //true
console.log( firstVar || 10 );      //true
console.log( firstVar || '10' );    //true
Logical NOT operator – ! It is used to reverse the logical state of its operand. It converts true to false, and vice-versa.

let firstVar = 10;
let secondVar = 20;
 
console.log( !true );   //false
console.log( !false );    //true
console.log( !firstVar ); //false
console.log( !null );   //true

Happy Learning !!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of


0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.