JS

논리 연산자 - OR, And, Not

hwongje 2024. 4. 17. 09:00

논리 연산자

 

OR 연산자 ( || )

OR 연산자는 입력값에 true가 1개 이상 나올 때 결과가 true로 출력된다.

let x = 1;
let y = 2;

console.log((x > y) || (x < y)); // (x > y)는 false지만 (x < y)가 true여서 true 출력
console.log((x > y) || (x == y)); // 둘다 false기때문에 false 출력

 

입력값 A 입력값 B 결과
true true true
true false true
false true true
false false false

 

 

And 연산자 ( && )

And 연산자는 입력값들이 모두 true가 나온다면 결과가 true로 출력된다.

let x = 1;
let y = 2;

console.log((x == y) && (x < y)); // false
console.log((x != y) && (x < y)); // true
// 입력값이 모두 true일 경우에만 결과값이 true로 출력된다.

 

 

입력값 A 입력값 B 결과
true true true
true false false
false true false
false false false

 

Not 연산자 ( ! )

Not 연산자는 부정연산자로 입력값들을 반대로 출력하게 된다.

let x = 1;
let y = 1;
let z = 2;

console.log(x == y;) // true
console.log(x != y;) // false
console.log((x+y) != z;) // false