javascript

[ javascript] 02. 연산자_논리연산자

쿨키드 2022. 8. 26. 16:44
논리 연산자(logical operator)
논리 연산자는 주어진 논리식을 판단하여, 참(true)과 거짓(false)을 반환
&& 연산자와 || 연산자는 두 개의 피연산자를 가지는 이항 연산자이며, 피연산자들의 결합 방향은 왼쪽에서 오른쪽입
! 연산자는 피연산자가 단 하나뿐인 단항 연산자이며, 피연산자의 결합 방향은 오른쪽에서 왼쪽입니다.

&& : 논리식이 모두 참이면 참을 반환함. (논리 AND 연산)

|| : 논리식 중에서 하나라도 참이면 참을 반환함. (논리 OR 연산)

! : 논리식의 결과가 참이면 거짓을, 거짓이면 참을 반환함. (논리 NOT 연산)


<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2022.07.12. 연산자03_논리연산자</title>
</head>

<body>
    <script>
        let 참 = true;
        let 거짓 = false;
        const a = 'ㅇㄷㅇ'

        console.log(참, 거짓);
        console.log(!참, !거짓); // ! not 부정연산자

        console.log(참 && 거짓, 참 && 참, 거짓 && 거짓); // AND연산자. 하나라도 거짓이면 거짓. 둘 다 참일 때만 참.
        console.log(거짓 || 참, 거짓 || 거짓); // || OR연산자. 허나라도 참이면 참. 

        a ? console.log(a + ' 진실한 너') : console.log('거짓된 너');
        // if 버전
        if (!a) {
            console.log(a + ' 진실한 너')
        } else {
            console.log('거짓된 너')
        }


        a && console.log(a + ' 진실한 너');
        // if 버전
        if (a) {
            console.log(a + ' 진실한 너')
        }

        !a || console.log('거짓된 너');
        a || console.log('거짓된 너'); //a가 참이어서, console.log('거짓된 너')은 없는 걸로 친다. 그래서 console에 찍히는 게 없음.

        const Test = () => {
            return (
                a ? <h2>hello</h2> : <h2>bye</h2>
            )
        }
    </script>
</body>

</html>