본문 바로가기

Javascript

조건 연산자(삼항연산자) 조건? true:false

조건? true:false

true, false부분 타입 같아야 합니다!

 

아래에 코드가 있어요!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>조건 연산</h3><hr>
    <script>
        var a = 3, b = 5;
        document.writeln((a>b)?a:b);
        document.writeln((a>b)?"a가 큽니다.":"b가 큽니다.");
    </script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

?가 들어간다.

true면 앞의 값(a), false면 뒤의 값(b)이 출력된다.

두개의 타입이 동일해야한다(정수면 정수, 문자면 문자)

 

출력값