코밍이의 하루

[JS] 비교 연산자, 논리 연산자, 연산자 우선순위 본문

웹언어 공부/JS

[JS] 비교 연산자, 논리 연산자, 연산자 우선순위

코밍이 2021. 8. 10. 00:46

1. 개발 환경

구분 내용
사용 언어 HTML5, JS
개발환경 Visual Studio Code
참고 도서 [Do it] 자바스크립트 + 제이쿼리 입문
웹브라우저 Chrome

 

2. 주요 문법

1) 비교 연산자

- 두 데이터를 비교할 때 사용하는 연산자

- 연산된 결과값은 논리형 데이터(true, false)를 반환한다.

종류 설명
A > B A가 B보다 크다.
A < B A가 B보다 작다.
A >= B A가 B보다 크거나 같다.
A <= B A가 B보다 작거나 같다.
A == B A와 B는 같다.(자료형 미포함)
A != B A와 B는 다르다.(자료형 미포함)
A === B A와 B는 같다.(자료형 포함)
A !== B A와 B는 다르다.(자료형 포함)

 

2) 논리 연산자

종류 설명
|| or 연산자, 피연산자 중 값이 하나라도 true가 존재하면 true로 결과값 반환
&& and 연산자, 피연산자 중 값이 하나라도 false가 존재하면 false로 결과값 반환
! not 연산자, 단항 연산자이며 피연삱의 값이 true이면 false 결과값 반환

3) 연산자 우선순위

① ()
② 단항 연산자( --, ++, ! )
③ 산술 연산자( *, /, %, +, - )
④ 비교 연산자( >, >=, <, <=, ==, ===, !==, != )
⑤ 논리 연산자( &&, ||)
⑥ 대입(복합 대입)연산자( =, +=, -=, *=, /=, %= )

 

3. 소스 코드 및 실행 결과

1) 비교연산자 

- part-2-5-5.html

 
<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<title> 비교 연산자 </title>
	<script>
		var a = 10;
		var b = 20;
		var c = 10;
		var f = "20";
		var result;

		result = a > b;  // false
		document.write(result, "<br>");
		result = a < b;  // true
		document.write(result, "<br>");
		result = a <= b; // true
		document.write(result, "<br>");
		result = b == f;  // true
		document.write(result, "<br>");
		result = a != b;   // true
		document.write(result, "<br>");
		result = b === f;  // false
		document.write(result, "<br>");
	</script>
</head>
<body>
</body>
</html>

part-2-5-5.html

 

2) 연산자 우선순위

- prior_c.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title> 논리연산자 </title>
<script type="text/javascript">
   var a=10;
   var b=20;
   var m=30;
   var n=40;
 
   var result;
   result= a>b || b>=m || m>n;     // false || false || false 
   document.write(result,"<br />");  // false
 
   result= a>b || b>=m || m<=n;   // false || false || true
   document.write(result,"<br />"); // true
 
   result= a<=b && b>=m && m<=n; // true && false && true
   document.write(result,"<br />"); // false
 
   result= a<=b && b<=m && m<=n; // true && true && true
   document.write(result,"<br />"); // true
 
   result= !(a>b);  // !false
   document.write(result,"<br />"); // true
</script>
</head>
<body>

</body>
</html>

prior_c.html