본문 바로가기

개발

자바스크립트 실습3 : 연산자

■operator-1.html 코드
<!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>연산자</title>

</head>
<body>
    <script>
        var radius = 10;
        var pi = 3.14;
        //alert함수는 알림창과 같은 것을 띄워준다.
        alert(2 * pi * radius)
    </script>
</body>
</html>

 

■operator-2.html 코드

<!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>연산자</title>

</head>
<body>
    <script>
        window.onload = function () {
            var list = ""; //문자열 타입
            var num = 10; //Number 타입
            // 복합대입 연산자를 통하여 list변수에 문자열을 누적하고 있다.
            list += "<ul>";
            list += "   <li>안녕</li>";
            list += "   <li>자바스크립트</li>";
            list += "</ul>";
            // 정수형도 아래와 같이 복합대입 연산을 통해서 값을 누적시키고 있다.
            num *= 10; num *= 10;
            num *= 10; num += 10;
            // 문서에 출력하라.
            // document.body.innerHTML = list;
            // document.body.innerHTML = num;
            document.write(num);

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

 

■operator-3.html 코드

<!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>연산자</title>

</head>
<body>
    <script>
        var num = 10;
        // 증감연산자를 사용함
        alert(num++);    //후위 증감연산자 : ;이 끝난 후 증가 됨
        alert(++num);    //전위 증감연산자 : ;이 끝나기 전에 이미 증가가 되어있다.
        alert(num++);
        document.write(num);
    </script>
</body>
</html>

 

■inputNum.html 코드

<!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>숫자 입력</title>
</head>
<body>
    <script>
        // prompt를 통해서 입력을 받으면 String타입으로 된다.
        var input = prompt("숫자를 입력해주세요", "숫자")
        alert(typeof(input));
        
        // Number()를 통해서 String을 숫자로 바꾸고 있다.
        var numType = Number(input);
        alert(typeof(numType));
        
        //NaN (Not a Number) 숫자가 될 수 없는 것을 뜻한다.
        alert(numType);
    </script>
</body>
</html>