개발나라 스용공주

[생활코딩] JavaScript 왕기초 5장 - Visual Studio Code / 조건문 본문

WEB/JavaScript

[생활코딩] JavaScript 왕기초 5장 - Visual Studio Code / 조건문

스용공주 2024. 8. 22. 17:19
728x90
728x90

이번 글은 아래 강의 내용을 바탕으로 작성된 글입니다.

(생활코딩)

https://www.youtube.com/watch?v=wjlbT3hvDMc&list=PLuHgQVnccGMBB348PWRN0fREzYcYgFybf&index=16

 

 


 

 

[ 조건문 if ]

아래 실습은 조건문 if에 대한 내용이다.

if는 결과값이 true면 첫줄을 if에 해당하는 첫번째 부분을 실행시키고 false면 그 다음 부분인 else에 해당하는 부분을 실행시킨다.

그래서 if의 괄호 안에는 boolean데이터 타입이 작성된다.

그래서 아래 실행결과와 같이 1 2 3 4 순으로 출력되어야할 문자가 true일 경우 첫번째만 실행하여 1 2 4가 출력되고 false의 경우 첫번째가 아닌 else부분인 그 다음을 실행하여 1 3 4가 출력된다.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
    </style>
</head>
<body>
    <h1>Conditional Statements</h1>

    <h2>Print Number</h2>
    <script>
        document.write("1<br>");
        document.write("2<br>");
        document.write("3<br>");
        document.write("4<br>");
    </script>

    <h2>If-True</h2>
    <script>
        document.write("1<br>");
        if(true) {
            document.write("2<br>");
        }
        else {
            document.write("3<br>");
        }
        document.write("4<br>");
    </script>

    <h2>If-False</h2>
    <script>
        document.write("1<br>");
        if(false) {
            document.write("2<br>");
        }
        else {
            document.write("3<br>");
        }
        document.write("4<br>");
</script>
</body>
</html>

 

 


728x90
728x90
Comments