JavaScript

[JavaScript] 연산자

퓨어맨 2022. 5. 27. 01:01
<!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>ex05연산자</title>
</head>
<body>
    <script>
        // Java와 모든 연산자들이 동일하다.
        // 단, 비교연산자 == , === 만 다르다.
        // == (동등연산자)
        // : 양쪽 항의 자료형이 다를 때 자동 형변환 비교
        // : 안쪽에 들어있는 데이터만 같다면 true를 되돌려줌
        console.log(5=='5')

        // === (일치연산자)
        // : 양쪽 항의 자료형을 엄격히 검사한다
        console.log(5==='5')





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

5 == '5' : true

5 === '5' : false

 

<!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>ex06연산자실습</title>
</head>
<body>
    <script>
        let num = prompt("숫자 입력")
        let result = num - num%100

        // 1. num에서 백의자리 이하의 숫자는 버리기
        // - 백의 자리 숫자만 뽑아오기
        // 1-1) console.log(num/100);
        // --> javascript에서는 실수, 정수를 number 자료형으로 간주하기 때문에
        // --> 자동으로 실수형태의 데이터로 나온다.

        // 1-2) 실수형태를 정수형으로 변환
        console.log(parseInt(num/100))
        
        // 2. 콘솔창에 출력
        console.log("백의 자리 이하 버린결과 >> "+result)




        // javascript에서의 형변환
        /*
            1. 정수로 형변환
            : 실수형 데이터나 문자열 데이터 두개 다 들어올 수 있다.
            parseInt('3') , parseInt(3.14)
            
            2. 실수로 형변환
            : 문자열 데이터나 정수형 데이터 두개 다 들어올 수 있다.
            parseFloat('3.14') , parseFloat(3)

            3. 다른 자료형을 숫자형태로 형변환
            Number('123456')

            4. 숫자를 문자열로 변경
            toString()
        */
    </script>
</body>
</html>