본문 바로가기

예습공부

Java Script 기초 강의 5 - by. 코딩앙마

5. 연산자

 

 

1) 기본연산자

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

[보충] 감소연산자의 경우

let num = 10;

   let result = num--; (변화X) >> console.log(result); 10

   let result = --num; (감소한 값이 결과에 포함, 변화O) >> console.log(result); 9

 

* ++, -- 부호가 두 번 붙었다고 해서 +2, -2가 아니라 1씩 증가, 감소

 

 

2) 비교연산자, 조건문

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

 

3) 논리연산자

  • || (or) ; 여러개 중 하나라도 true면 true, 모든값이 false일때만 false를 반환
  • && (and) ; 모든값이 true여야 true, 하나라도 false면 false를 반환
  • ! (not) ; true/false를 반대값으로 반환

 

  •  논리연산자의 '평가', 그에 따른 배치방법

    -  ||는 첫번째 true를 발견하는 즉시 평가를 멈춤

    -  &&는 첫번째 false를 발견하는 즉시 평가를 멈춤

        ex) 운전면허가 있고 시력이 좋은 여군을 선발하는 프로그램

   data>>   80%               60%           7%

           => 여군인데 시력이 좋고 운전면허가 있는 사람

               ; 93%의 여군이 아닌 지원자를 먼저 걸러낼 수 있다. (성능 최적화에 도움)

 

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.

 

 

4) 비교연산자의 우선순위

 

See the Pen Untitled by Eun-yeong Shin (@Rachael_S) on CodePen.