TypeScript 逻辑运算符

TypeScript 逻辑运算符

原文: https://howtodoinjava.com/typescript/logical-operators/

TypeScript 逻辑运算符类似于您在 JavaScript 逻辑运算符中学习的内容。 这些运算符有助于比较布尔表达式并产生单个布尔值作为结果。

逻辑运算符

逻辑 AND 运算符 – &&

如果两个操作数(或表达式)均为true,则结果将为true,否则为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'

阅读更多:真值和假值

逻辑或运算符 – ||

如果两个操作数(或表达式)中的任何一个为true,则结果将为true,否则为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

逻辑非运算符 – !

它用于反转其操作数的逻辑状态。 它将true转换为false,反之亦然。

			let firstVar = 10;
			let secondVar = 20;

			console.log( !true );		//false
			console.log( !false );		//true
			console.log( !firstVar );	//false
			console.log( !null );		//true

将我的问题放在评论部分。

学习愉快!