JavaScript-연산자

운영자는 무엇입니까?

간단한 표현을합시다 4 + 5 is equal to 9. 여기서 4와 5는operands 그리고 '+'는 operator. JavaScript는 다음 유형의 연산자를 지원합니다.

  • 산술 연산자
  • 비교 연산자
  • 논리 (또는 관계형) 연산자
  • 할당 연산자
  • 조건부 (또는 삼항) 연산자

모든 연산자를 하나씩 살펴 보겠습니다.

산술 연산자

JavaScript는 다음과 같은 산술 연산자를 지원합니다.

변수 A가 10을 보유하고 변수 B가 20을 보유한다고 가정하면-

Sr. 아니. 연산자 및 설명
1

+ (Addition)

두 개의 피연산자를 더합니다.

Ex: A + B는 30을 줄 것입니다

2

- (Subtraction)

첫 번째에서 두 번째 피연산자를 뺍니다.

Ex: A-B는 -10을 줄 것입니다.

삼

* (Multiplication)

두 피연산자 곱하기

Ex: A * B는 200을 줄 것입니다

4

/ (Division)

분자를 분모로 나눕니다.

Ex: B / A는 2를 줄 것입니다

5

% (Modulus)

정수 나눗셈의 나머지를 출력합니다.

Ex: B % A는 0을 제공합니다.

6

++ (Increment)

정수 값을 1 씩 증가시킵니다.

Ex: A ++는 11을 줄 것입니다

7

-- (Decrement)

정수 값을 1 씩 줄입니다.

Ex: A-- 9를 줄 것입니다

Note− 더하기 연산자 (+)는 문자열뿐만 아니라 숫자에서도 작동합니다. 예를 들어 "a"+ 10은 "a10"을 제공합니다.

예

다음 코드는 JavaScript에서 산술 연산자를 사용하는 방법을 보여줍니다.

<html>
   <body>
   
      <script type = "text/javascript">
         <!--
            var a = 33;
            var b = 10;
            var c = "Test";
            var linebreak = "<br />";
         
            document.write("a + b = ");
            result = a + b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a - b = ");
            result = a - b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a / b = ");
            result = a / b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a % b = ");
            result = a % b;
            document.write(result);
            document.write(linebreak);
         
            document.write("a + b + c = ");
            result = a + b + c;
            document.write(result);
            document.write(linebreak);
         
            a = ++a;
            document.write("++a = ");
            result = ++a;
            document.write(result);
            document.write(linebreak);
         
            b = --b;
            document.write("--b = ");
            result = --b;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>
      
      Set the variables to different values and then try...
   </body>
</html>

산출

a + b = 43
a - b = 23
a / b = 3.3
a % b = 3
a + b + c = 43Test
++a = 35
--b = 8
Set the variables to different values and then try...

비교 연산자

JavaScript는 다음과 같은 비교 연산자를 지원합니다.

변수 A가 10을 보유하고 변수 B가 20을 보유한다고 가정하면-

Sr. 아니. 연산자 및 설명
1

= = (Equal)

두 피연산자의 값이 같은지 확인합니다. 그렇다면 조건이 참이됩니다.

Ex: (A == B)는 사실이 아닙니다.

2

!= (Not Equal)

두 피연산자의 값이 같은지 확인하고, 값이 같지 않으면 조건이 참이됩니다.

Ex: (A! = B)는 참입니다.

삼

> (Greater than)

왼쪽 피연산자의 값이 오른쪽 피연산자의 값보다 큰지 확인하고, 그렇다면 조건이 참이됩니다.

Ex: (A> B)는 사실이 아닙니다.

4

< (Less than)

왼쪽 피연산자의 값이 오른쪽 피연산자의 값보다 작은 지 확인하고, 그렇다면 조건이 참이됩니다.

Ex: (A <B)는 사실입니다.

5

>= (Greater than or Equal to)

왼쪽 피연산자의 값이 오른쪽 피연산자의 값보다 크거나 같은지 확인합니다. 그렇다면 조건이 참이됩니다.

Ex: (A> = B)는 사실이 아닙니다.

6

<= (Less than or Equal to)

왼쪽 피연산자의 값이 오른쪽 피연산자의 값보다 작거나 같은지 확인합니다. 그렇다면 조건이 참이됩니다.

Ex: (A <= B)는 참입니다.

예

다음 코드는 JavaScript에서 비교 연산자를 사용하는 방법을 보여줍니다.

<html>
   <body>  
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
      
            document.write("(a == b) => ");
            result = (a == b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a < b) => ");
            result = (a < b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a > b) => ");
            result = (a > b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a != b) => ");
            result = (a != b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a >= b) => ");
            result = (a >= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a <= b) => ");
            result = (a <= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      Set the variables to different values and different operators and then try...
   </body>
</html>

산출

(a == b) => false 
(a < b) => true 
(a > b) => false 
(a != b) => true 
(a >= b) => false 
a <= b) => true
Set the variables to different values and different operators and then try...

논리 연산자

JavaScript는 다음과 같은 논리 연산자를 지원합니다.

변수 A가 10을 보유하고 변수 B가 20을 보유한다고 가정하면-

Sr. 아니. 연산자 및 설명
1

&& (Logical AND)

두 피연산자가 모두 0이 아니면 조건이 참이됩니다.

Ex: (A && B)는 사실입니다.

2

|| (Logical OR)

두 피연산자 중 하나가 0이 아니면 조건이 참이됩니다.

Ex: (A || B)는 사실입니다.

삼

! (Logical NOT)

피연산자의 논리적 상태를 되돌립니다. 조건이 참이면 논리 NOT 연산자는 조건을 거짓으로 만듭니다.

Ex:! (A && B)는 거짓입니다.

예

JavaScript에서 논리 연산자를 구현하는 방법을 알아 보려면 다음 코드를 시도하십시오.

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = true;
            var b = false;
            var linebreak = "<br />";
      
            document.write("(a && b) => ");
            result = (a && b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a || b) => ");
            result = (a || b);
            document.write(result);
            document.write(linebreak);
         
            document.write("!(a && b) => ");
            result = (!(a && b));
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

산출

(a && b) => false 
(a || b) => true 
!(a && b) => true
Set the variables to different values and different operators and then try...

비트 연산자

JavaScript는 다음과 같은 비트 연산자를 지원합니다.

변수 A에 2가 있고 변수 B에 3이 있다고 가정하면-

Sr. 아니. 연산자 및 설명
1

& (Bitwise AND)

정수 인수의 각 비트에 대해 부울 AND 연산을 수행합니다.

Ex: (A & B)는 2입니다.

2

| (BitWise OR)

정수 인수의 각 비트에 대해 부울 OR 연산을 수행합니다.

Ex: (A | B)는 3입니다.

삼

^ (Bitwise XOR)

정수 인수의 각 비트에 대해 부울 배타적 OR 연산을 수행합니다. 배타적 OR은 피연산자 1이 참이거나 피연산자 2가 참이지만 둘다는 아니라는 것을 의미합니다.

Ex: (A ^ B)는 1입니다.

4

~ (Bitwise Not)

단항 연산자이며 피연산자의 모든 비트를 반전하여 작동합니다.

Ex: (~ B)는 -4입니다.

5

<< (Left Shift)

첫 번째 피연산자의 모든 비트를 두 번째 피연산자에 지정된 자릿수만큼 왼쪽으로 이동합니다. 새 비트는 0으로 채워집니다. 값을 한 위치 왼쪽으로 이동하는 것은 2를 곱하는 것과 같고, 두 위치를 이동하는 것은 4를 곱하는 것과 같습니다.

Ex: (A << 1)은 4입니다.

6

>> (Right Shift)

이진 오른쪽 시프트 연산자. 왼쪽 피연산자의 값은 오른쪽 피연산자가 지정한 비트 수만큼 오른쪽으로 이동합니다.

Ex: (A >> 1)은 1입니다.

7

>>> (Right shift with Zero)

이 연산자는 >> 연산자와 비슷하지만 왼쪽에서 이동 된 비트가 항상 0이라는 점이 다릅니다.

Ex: (A >>> 1)은 1입니다.

예

JavaScript에서 Bitwise 연산자를 구현하려면 다음 코드를 시도하십시오.

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 2; // Bit presentation 10
            var b = 3; // Bit presentation 11
            var linebreak = "<br />";
         
            document.write("(a & b) => ");
            result = (a & b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a | b) => ");
            result = (a | b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a ^ b) => ");
            result = (a ^ b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(~b) => ");
            result = (~b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a << b) => ");
            result = (a << b);
            document.write(result);
            document.write(linebreak);
         
            document.write("(a >> b) => ");
            result = (a >> b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>
(a & b) => 2 
(a | b) => 3 
(a ^ b) => 1 
(~b) => -4 
(a << b) => 16 
(a >> b) => 0
Set the variables to different values and different operators and then try...

할당 연산자

JavaScript는 다음 할당 연산자를 지원합니다-

Sr. 아니. 연산자 및 설명
1

= (Simple Assignment )

오른쪽 피연산자의 값을 왼쪽 피연산자에 할당합니다.

Ex: C = A + B는 A + B의 값을 C에 할당합니다.

2

+= (Add and Assignment)

왼쪽 피연산자에 오른쪽 피연산자를 추가하고 결과를 왼쪽 피연산자에 할당합니다.

Ex: C + = A는 C = C + A와 같습니다.

삼

−= (Subtract and Assignment)

왼쪽 피연산자에서 오른쪽 피연산자를 빼고 결과를 왼쪽 피연산자에 할당합니다.

Ex: C-= A는 C = C-A와 같습니다.

4

*= (Multiply and Assignment)

오른쪽 피연산자와 왼쪽 피연산자를 곱하고 결과를 왼쪽 피연산자에 할당합니다.

Ex: C * = A는 C = C * A와 같습니다.

5

/= (Divide and Assignment)

왼쪽 피연산자를 오른쪽 피연산자로 나누고 결과를 왼쪽 피연산자에 할당합니다.

Ex: C / = A는 C = C / A와 같습니다.

6

%= (Modules and Assignment)

두 개의 피연산자를 사용하여 모듈러스를 취하고 결과를 왼쪽 피연산자에 할당합니다.

Ex: C % = A는 C = C % A와 같습니다.

Note − 동일한 논리가 비트 연산자에도 적용되므로 << =, >> =, >> =, & =, | = 및 ^ =처럼됩니다.

예

JavaScript에서 할당 연산자를 구현하려면 다음 코드를 시도하십시오.

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 33;
            var b = 10;
            var linebreak = "<br />";
         
            document.write("Value of a => (a = b) => ");
            result = (a = b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a += b) => ");
            result = (a += b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a -= b) => ");
            result = (a -= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a *= b) => ");
            result = (a *= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a /= b) => ");
            result = (a /= b);
            document.write(result);
            document.write(linebreak);
         
            document.write("Value of a => (a %= b) => ");
            result = (a %= b);
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

산출

Value of a => (a = b) => 10
Value of a => (a += b) => 20 
Value of a => (a -= b) => 10 
Value of a => (a *= b) => 100 
Value of a => (a /= b) => 10
Value of a => (a %= b) => 0
Set the variables to different values and different operators and then try...

기타 연산자

여기서는 JavaScript에서 매우 유용한 두 가지 연산자에 대해 설명합니다. conditional operator (? :) 그리고 typeof operator.

조건부 연산자 (? :)

조건부 연산자는 먼저 표현식에서 참 또는 거짓 값을 평가 한 다음 평가 결과에 따라 주어진 두 명령문 중 하나를 실행합니다.

Sr. 아니. 연산자 및 설명
1

? : (Conditional )

조건이 참이면? 그런 다음 값 X : 그렇지 않으면 값 Y

예

조건부 연산자가 JavaScript에서 작동하는 방식을 이해하려면 다음 코드를 시도하십시오.

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
         
            document.write ("((a > b) ? 100 : 200) => ");
            result = (a > b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         
            document.write ("((a < b) ? 100 : 200) => ");
            result = (a < b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

산출

((a > b) ? 100 : 200) => 200 
((a < b) ? 100 : 200) => 100
Set the variables to different values and different operators and then try...

typeof 연산자

그만큼 typeofoperator는 모든 유형이 될 수있는 단일 피연산자 앞에 배치되는 단항 연산자입니다. 값은 피연산자의 데이터 유형을 나타내는 문자열입니다.

과 typeof 의 피연산자가 숫자, 문자열, 부울 값 반환이 평가에 따라 true 또는 false 인 경우 "부울" "수", "문자열", 또는에 운영자 평가한다.

다음은 반환 값 목록입니다. typeof 운영자.

유형 typeof에 의해 반환되는 문자열
번호 "번호"
끈 "끈"
부울 "부울"
목적 "목적"
함수 "함수"
찾으시는 주소가 없습니다 "찾으시는 주소가 없습니다"
없는 "목적"

예

다음 코드는 구현 방법을 보여줍니다. typeof 운영자.

<html>
   <body>      
      <script type = "text/javascript">
         <!--
            var a = 10;
            var b = "String";
            var linebreak = "<br />";
         
            result = (typeof b == "string" ? "B is String" : "B is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
         
            result = (typeof a == "string" ? "A is String" : "A is Numeric");
            document.write("Result => ");
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

산출

Result => B is String 
Result => A is Numeric
Set the variables to different values and different operators and then try...