Logical AND(&&) 와 Logical OR(||) 연산자
[TypeScript] && 와 || 연산자
섹션 제목: “[TypeScript] && 와 || 연산자” author: Onejay createdAt: 2021-10-13 updatedAt: 2021-10-13&&와 || 연산자는 두 피연산자 중 하나의 값 을 반환한다.
| L-Operand | True | False |
|---|---|---|
| && | R-Operand 값 | L-Operand 값 |
| || | L-Operand 값 | R-Operand 값 |
(※ L-Operand: 왼쪽 피연산자, R-Operand: 오른쪽 피연산자)
- && 는 True를 기준으로 오른쪽 피연산자의 값을 반환
- || 는 False를 기준으로 오른쪽 피연산자의 값을 반환
Logical AND(&&) 연산자
섹션 제목: “Logical AND(&&) 연산자”-
왼쪽의 피연산자(L-Operand)가 True(참)인 경우, 오른쪽 피연산자(R-Operand) 값을 반환한다.
const val: string = `VALUE TEST`;const isExist: boolean = true;console.log(`${isExist && val}`);[Result]VALUE TEST -
왼쪽의 피연산자(L-Operand)가 False(거짓)인 경우, 왼쪽 피연산자 값을 반환한다.
const val: string = `VALUE TEST`;const isExist: boolean = false;console.log(`${isExist && val}`);[Result]false
Logical OR(||) 연산자
섹션 제목: “Logical OR(||) 연산자”-
왼쪽의 피연산자(L-Operand)가 True 인 경우, 왼쪽 피연산자 값을 반환
const val: string = `VALUE TEST`;const isExist: boolean = true;console.log(`${isExist || val}`);[Result]true -
왼쪽의 피연산자(L-Operand)가 False 인 경우, 오른쪽 피연산자(R-Operand) 값을 반환한다.
const val: string = `VALUE TEST`;const isExist: boolean = false;console.log(`${isExist || val}`);[Result]VALUE TEST
False에 해당하는 데이터
섹션 제목: “False에 해당하는 데이터”-
숫자(number):
0const zeroNum: number = 0;console.log(`Zero Number - Boolean: ${zeroNum ? 'TRUE' : 'FALSE'}`);const pNum: number = 1;console.log(`Number - Boolean: ${pNum ? 'TRUE' : 'FALSE'}`);const minuNum: number = 1;console.log(`Minus Number - Boolean: ${minuNum ? 'TRUE' : 'FALSE'}`);[Result]Zero Number - Boolean: FALSENumber - Boolean: TRUEMinus Number - Boolean: TRUE -
문자열(string):
빈 문자열(길이가 0인 문자열)const emptyStr: string = '';console.log(`Empty String - length: ${emptyStr.length}, Boolean: ${emptyStr ? 'TRUE' : 'FALSE'}`);const str: string = 'Not Empty';console.log(`Not Empty String - length: ${str.length}, Boolean: ${str ? 'TRUE' : 'FALSE'}`);[Result]Empty String - length: 0, Boolean: FALSENot Empty String - length: 9, Boolean: TRUE -
객체(object):
null, undefinedconst nullObj: any = null;console.log(`Null Object - Boolean: ${nullObj ? 'TRUE' : 'FALSE'}`);const undefinedObj: any = undefined;console.log(`Undefined Object - Boolean: ${undefinedObj ? 'TRUE' : 'FALSE'}`);const emptyObj: any = {};console.log(`Empty Object - length: ${emptyObj.length}, Boolean: ${emptyObj ? 'TRUE' : 'FALSE'}`);[Result]Null Object - Boolean: FALSEUndefined Object - Boolean: FALSEEmpty Object - length: undefined, Boolean: TRUE