JavaScript(8)

박찬영·2024년 1월 15일

JavaScript

목록 보기
8/19

1. 비교

• 자바스크립트는 주어진 두 항을 비교할 수 있는 '비교 연산자'를 제공한다
• 자바스크립트에서는 다음 두 가지 유형의 비교를 할 수 있다.
  → 크냐 작쟈 (대소 비교)
  	같냐 다르냐 (등가 비교)
     
     

2. 비교 연산의 특징

• 비교 연산식은 언제나 boolean 데이터를 반환한다.
• 크냐 작냐를 비교하는 대소 비교, 같냐 다르냐를 비교하는 등가 비교는 모두 하나의 질문이며, 
  질문에 대한 답이 참(true) 또는 거짓(false)인 것이다.

3. 대소 비교

4. 등가 비교

	* 등가 비교를 할 때, 등호(=)의 개수에 따라 비교 규칙에 차이를 보인다.
	  ==는 '추상적 같음 비교'로써, 자료형이 서로 다르더라도 같다고 판단할 수 있는 비교이다.
	  ===는 '엄격한 같음 비교'로써, 자료형과 데이터가 모두 일치해야만 같다고 판단한다.
      

      ex) console.log('1' == 1) 		//true
	      console.log('1' === 1)		//false
    
    

5. 실습

1) html 코드

<!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, inital-scale=1">
        <title>자바스크립트 실습</title>
    </head>
    <body>

        <script src="script.js"></script>
    </body>
</html>

2) js 코드

let a = 3;
let b = 5;
let c = '3';

console.log( a >= b );
console.log( a <= b ); // <, > 먼저 사용

console.log( a == c);
console.log( a === c);

console.log( a != c);
console.log( a !== c);

3) 결과

profile
블로그 이전했습니다 -> https://young-code.tistory.com

0개의 댓글