패스트캠퍼스
javaScript-논리 연산자
용용it
2023. 4. 17. 17:47
논리 연산자
논리 연산자는 조건을 평가하고 그 값을 boolean 데이터로 나타내는 연산자이다.
주로 조건문이나 반복문에서 사용.
AND(그리고) 연산자 && : 두개의 피연산자가 모두 ture 일 때만 true를 반환하고 이외에는 모두 false를 반환한다.
OR(또는) 연산자 || : 두 개의 피연산자 중 하나만 true 이면 true를 반환하고, 두 개의 피연산자가 모두 false이면 false를 반환한다.
// AND(그리고) 연산자
const a = true
const b = false
if (a && b) {
console.log('모두가 참')
}
// x
// OR(또는) 연산자
if (a || b) {
console.log('하나 이상이 참')
}
// 하나 이상이 참
// AND 연산자
console.log(true && false) // false
console.log(1 && 0) // 0
console.log(1 && 2 && 0) // 0
console.log(1 && 0 && 2) // 0
console.log(0 && 1 && 2) // 0
console.log('A' && 'B' && '') //
console.log('A' && 'B' && 'C') // 0
// OR 연산자
console.log(0 || 1) // 1
console.log(false || 0 || {}) // {}
console.log(false || [] || null) // []
console.log(function () {} || undefined || '') // f () {}
console.log(false || 0 || NaN) // NaN