javascript

논리연산자(||) 활용하기 : 매개변수 디폴트 할당

핑구뱅구 2022. 10. 4. 11:20

 - 논리 OR 연산자 
   이 연산자는 ->방향으로 연산을 진행하고 가장 먼저 참(true)의 형태를 가진 value가 나오는 경우
   그 피연산자를 바로 반환해버리고 연산을 끝내버립니다.

 

-  논리연산자 활용하기 : 매개변수 디폴트 할당

<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<body>
    <script>
        function test(num) {
            const n = num || 99;
            console.log(n);
        }

        test(3); // 3
        test();  // 99

    </script>
</body>
</html>

 

참조 : https://mynameisdabin.tistory.com/10