JavaScriptの演算子の種類

このチュートリアルでは、JavaScriptでの演算子の種類の数と、JavaScript演算子の使用方法を例とともに学習します。

例を使用したJavaScriptの演算子のタイプ

javascriptの演算子の種類:

  • 算術演算子
  • 比較演算子
  • 論理演算子
  • 代入演算子
  • 条件付き演算子

算術演算子

算術演算子は、数値オペランド間で数学演算を実行するために使用されます。

オペレーター説明
+2つの数値オペランドを追加します。
左のオペランドから右のオペランドを引く
*2つの数値オペランドを乗算します。
/左のオペランドを右のオペランドで除算します。
モジュラス演算子。2つのオペランドの余りを返します。
++インクリメント演算子。オペランド値を1つ増やします。
デクリメント演算子。値を1つ減らします。

例-JavaScript算術演算子

次の例は、算術演算子がオペランドに対してさまざまなタスクを実行する方法を示しています。

<html>
 
   <head>
     <title> JavaScript Arithmetic Operator Examples</title>
   </head>
 
   <body>
 
      <script>
 
            var x = 5;
            var y = 50;
            var z = 100;
            var newline = "<br />";
          
            document.write("x + y ===output===> ");
            output = x + y;
            document.write(output);
            document.write(newline);
          
            document.write("x - y ===output===> ");
            output = x - y;
            document.write(output);
            document.write(newline);
          
            document.write("x / y ===output===> ");
            output = x / y;
            document.write(output);
            document.write(newline);
          
            document.write("x % y ===output===> ");
            output = x % y;
            document.write(output);
            document.write(newline);
          
            document.write("x + y + z ===output===> ");
            output = x + y + z;
            document.write(output);
            document.write(newline);
          
            x = ++x;
            document.write("++x ===output===> ");
            output = ++x;
            document.write(output);
            document.write(newline);
          
            y = --y;
            document.write("--y ===output===> ");
            output = --y;
            document.write(output);
            document.write(newline);
          
      </script>
 
   </body>
 
</html>

Output:

x + y = 55
x-y = -45
x / y = 0.1
x%y = 5
x + y + z = 155
++ x = 7
--y = 48 

ノート

+ 演算子は、オペランドの1つに文字列値が含まれている場合に連結演算を実行します。

ここで、例は、+ 演算子がさまざまなデータ型のオペランドに対して操作を実行する方法を示してい ます。

var a = 50, b = "Hello ", c = 15;
 
a + b; // "5Hello "
 
a + c; // 65

比較演算子

JavaScript比較演算子は、2つのオペランド値を比較し、ブール値trueまたはfalseを返します。

演算子説明
==タイプを考慮せずに、2つのオペランドの等価性を比較します。
===2つのオペランドの等価性を型と比較します。
!=2つのオペランドの不等式を比較します。
>>左側の値が右側の値より大きいかどうかを確認します。はいの場合はtrueを返し、そうでない場合はfalseを返します。
<左のオペランドが右のオペランドよりも小さいかどうかをチェックします。はいの場合はtrueを返し、そうでない場合はfalseを返します。
> =左のオペランドが右のオペランド以上かどうかを確認します。はいの場合はtrueを返し、そうでない場合はfalseを返します。
<=左のオペランドが右のオペランド以下かどうかをチェックします。はいの場合はtrueを返し、そうでない場合はfalseを返します。

例–JavaScript比較演算子

次の例は、比較演算子がさまざまなタスクを実行する方法を示しています。

<html>
 
   <head>
    <title> JavaScript Comparison Operator Examples</title>
   </head>
 
   <body>
 
      <script type = "text/javascript">
 
            var x = 50;
            var y = 100;
            var breakLine = "<br />";
       
            document.write("(x == y) ===output===> ");
            output = (x == y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x < y) ===output===> ");
            output = (x < y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x > y) ===output===> ");
            output = (x > y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x != y) ===output===> ");
            output = (x != y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x >= y) ===output===> ");
            output = (x >= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x <= y) ===output===> ");
            output = (x <= y);
            document.write(output);
            document.write(breakLine);
   
      </script>     
 
   </body>
 
</html>

Output:

(x == y) ===output===> false
(x < y) ===output===> true
(x > y) ===output===> false
(x != y) ===output===> true
(x >= y) ===output===> false
(x <= y) ===output===> true 

論理演算子

論理演算子は、2つ以上の条件を組み合わせるために使用されます。JavaScriptには、次の論理演算子が含まれています。

オペレーター説明
&&&&はAND演算子として知られています。2つのオペランドがゼロ以外(0、false、undefined、null、または「」がゼロと見なされる)であるかどうかをチェックし、yesの場合は1を返し、そうでない場合は0を返します。
|||| OR演算子として知られています。2つのオペランドのいずれかがゼロ以外であるかどうかをチェックします(0、false、undefined、null、または「」はゼロと見なされます)。
!NOT演算子として知られています。オペランド(または条件)のブール結果を反転します

例–JavaScript論理演算子


<html>

  <head>
   <title> JavaScript Logical Operator Examples</title>
  </head>

  <body>

     <script type = "text/javascript">

           var x = 50;
           var y = 100;
           var breakLine = "<br />";
      
           
           document.write("(x && y) ===output===> ");
           output = (x && y);
           document.write(output);
           document.write(breakLine);
         
           document.write("(x || y) ===output===> ");
           output = (x || y);
           document.write(output);
           document.write(breakLine);
         
           document.write("!(x && y) ===output===> ");
           output = (!(x && y));
           document.write(output);
           document.write(breakLine);
  
     </script>     

  </body>

</html>

Output:

(x && y) ===output===> 100
(x || y) ===output===> 50
!(x && y) ===output===> false 

代入演算子

JavaScriptには、より少ないキーストロークで変数に値を割り当てるための代入演算子が含まれています。

代入演算子説明
=右のオペランド値を左のオペランドに割り当てます。
+ =左と右のオペランド値を合計し、その結果を左のオペランドに割り当てます。
-=左のオペランド値から右のオペランド値を減算し、その結果を左のオペランドに割り当てます。
* =左と右のオペランド値を乗算し、その結果を左のオペランドに割り当てます。
/ =左のオペランド値を右のオペランド値で除算し、その結果を左のオペランドに割り当てます。
%=左オペランドのモジュラスを右オペランドで除算し、結果のモジュラスを左オペランドに割り当てます。

例–代入演算子


<html>
 
   <head>
    <title> JavaScript Assignment Operator Examples</title>
   </head>
 
   <body>
 
      <script type = "text/javascript">
 
            var x = 50;
            var y = 100;
            var breakLine = "<br />";
       
         
            document.write("Value of a ===output===> (x = y) ===output===> ");
            output = (x = y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x += y) ===output===> ");
            output = (x += y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x -= y) ===output===> ");
            output = (x -= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x *= y) ===output===> ");
            output = (x *= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x /= y) ===output===> ");
            output = (x /= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x %= y) ===output===> ");
            output = (x %= y);
            document.write(output);
            document.write(breakLine);
   
      </script>     
 
   </body>
 
</html>

Output

Value of a ===output===> (x = y) ===output===> 100
Value of a ===output===> (x += y) ===output===> 200
Value of a ===output===> (x -= y) ===output===> 100
Value of a ===output===> (x *= y) ===output===> 10000
Value of a ===output===> (x /= y) ===output===> 100
Value of a ===output===> (x %= y) ===output===> 0

 

三項演算子

JavaScriptには、三項演算子と呼ばれる特別な演算子が含まれていますか?ある条件に基づいて変数に値を割り当てます。これは、if-else条件の短縮形のようなものです。

言い換えると、 条件付き(三項)演算子 は、3つのオペランドをとる唯一のJavaScript演算子です。この演算子は、if-elseステートメントのショートカットとして頻繁に使用されます。
 

構文:

<condition> ? <value1> : <value2>;

三項演算子は条件式で始まり、その後に続きますか?オペレーター。条件が真であることが判明した場合、2番目の部分(?の後および前:演算子)が実行されます。条件がfalseになると、3番目の部分(後🙂が実行されます)。

例–JavaScriptの三項演算子

var a = 10, b = 5;
 
var c = a > b? a : b; // value of c would be 10
var d = a > b? b : a; // value of d would be 5

結論

このチュートリアルでは、例を使用してJavaScript演算子を学習しました。

リンク:https://www.tutsmake.com/javascript-operators-by-example/

#javascript 

 

What is GEEK

Buddha Community

JavaScriptの演算子の種類

JavaScriptの演算子の種類

このチュートリアルでは、JavaScriptでの演算子の種類の数と、JavaScript演算子の使用方法を例とともに学習します。

例を使用したJavaScriptの演算子のタイプ

javascriptの演算子の種類:

  • 算術演算子
  • 比較演算子
  • 論理演算子
  • 代入演算子
  • 条件付き演算子

算術演算子

算術演算子は、数値オペランド間で数学演算を実行するために使用されます。

オペレーター説明
+2つの数値オペランドを追加します。
左のオペランドから右のオペランドを引く
*2つの数値オペランドを乗算します。
/左のオペランドを右のオペランドで除算します。
モジュラス演算子。2つのオペランドの余りを返します。
++インクリメント演算子。オペランド値を1つ増やします。
デクリメント演算子。値を1つ減らします。

例-JavaScript算術演算子

次の例は、算術演算子がオペランドに対してさまざまなタスクを実行する方法を示しています。

<html>
 
   <head>
     <title> JavaScript Arithmetic Operator Examples</title>
   </head>
 
   <body>
 
      <script>
 
            var x = 5;
            var y = 50;
            var z = 100;
            var newline = "<br />";
          
            document.write("x + y ===output===> ");
            output = x + y;
            document.write(output);
            document.write(newline);
          
            document.write("x - y ===output===> ");
            output = x - y;
            document.write(output);
            document.write(newline);
          
            document.write("x / y ===output===> ");
            output = x / y;
            document.write(output);
            document.write(newline);
          
            document.write("x % y ===output===> ");
            output = x % y;
            document.write(output);
            document.write(newline);
          
            document.write("x + y + z ===output===> ");
            output = x + y + z;
            document.write(output);
            document.write(newline);
          
            x = ++x;
            document.write("++x ===output===> ");
            output = ++x;
            document.write(output);
            document.write(newline);
          
            y = --y;
            document.write("--y ===output===> ");
            output = --y;
            document.write(output);
            document.write(newline);
          
      </script>
 
   </body>
 
</html>

Output:

x + y = 55
x-y = -45
x / y = 0.1
x%y = 5
x + y + z = 155
++ x = 7
--y = 48 

ノート

+ 演算子は、オペランドの1つに文字列値が含まれている場合に連結演算を実行します。

ここで、例は、+ 演算子がさまざまなデータ型のオペランドに対して操作を実行する方法を示してい ます。

var a = 50, b = "Hello ", c = 15;
 
a + b; // "5Hello "
 
a + c; // 65

比較演算子

JavaScript比較演算子は、2つのオペランド値を比較し、ブール値trueまたはfalseを返します。

演算子説明
==タイプを考慮せずに、2つのオペランドの等価性を比較します。
===2つのオペランドの等価性を型と比較します。
!=2つのオペランドの不等式を比較します。
>>左側の値が右側の値より大きいかどうかを確認します。はいの場合はtrueを返し、そうでない場合はfalseを返します。
<左のオペランドが右のオペランドよりも小さいかどうかをチェックします。はいの場合はtrueを返し、そうでない場合はfalseを返します。
> =左のオペランドが右のオペランド以上かどうかを確認します。はいの場合はtrueを返し、そうでない場合はfalseを返します。
<=左のオペランドが右のオペランド以下かどうかをチェックします。はいの場合はtrueを返し、そうでない場合はfalseを返します。

例–JavaScript比較演算子

次の例は、比較演算子がさまざまなタスクを実行する方法を示しています。

<html>
 
   <head>
    <title> JavaScript Comparison Operator Examples</title>
   </head>
 
   <body>
 
      <script type = "text/javascript">
 
            var x = 50;
            var y = 100;
            var breakLine = "<br />";
       
            document.write("(x == y) ===output===> ");
            output = (x == y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x < y) ===output===> ");
            output = (x < y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x > y) ===output===> ");
            output = (x > y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x != y) ===output===> ");
            output = (x != y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x >= y) ===output===> ");
            output = (x >= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("(x <= y) ===output===> ");
            output = (x <= y);
            document.write(output);
            document.write(breakLine);
   
      </script>     
 
   </body>
 
</html>

Output:

(x == y) ===output===> false
(x < y) ===output===> true
(x > y) ===output===> false
(x != y) ===output===> true
(x >= y) ===output===> false
(x <= y) ===output===> true 

論理演算子

論理演算子は、2つ以上の条件を組み合わせるために使用されます。JavaScriptには、次の論理演算子が含まれています。

オペレーター説明
&&&&はAND演算子として知られています。2つのオペランドがゼロ以外(0、false、undefined、null、または「」がゼロと見なされる)であるかどうかをチェックし、yesの場合は1を返し、そうでない場合は0を返します。
|||| OR演算子として知られています。2つのオペランドのいずれかがゼロ以外であるかどうかをチェックします(0、false、undefined、null、または「」はゼロと見なされます)。
!NOT演算子として知られています。オペランド(または条件)のブール結果を反転します

例–JavaScript論理演算子


<html>

  <head>
   <title> JavaScript Logical Operator Examples</title>
  </head>

  <body>

     <script type = "text/javascript">

           var x = 50;
           var y = 100;
           var breakLine = "<br />";
      
           
           document.write("(x && y) ===output===> ");
           output = (x && y);
           document.write(output);
           document.write(breakLine);
         
           document.write("(x || y) ===output===> ");
           output = (x || y);
           document.write(output);
           document.write(breakLine);
         
           document.write("!(x && y) ===output===> ");
           output = (!(x && y));
           document.write(output);
           document.write(breakLine);
  
     </script>     

  </body>

</html>

Output:

(x && y) ===output===> 100
(x || y) ===output===> 50
!(x && y) ===output===> false 

代入演算子

JavaScriptには、より少ないキーストロークで変数に値を割り当てるための代入演算子が含まれています。

代入演算子説明
=右のオペランド値を左のオペランドに割り当てます。
+ =左と右のオペランド値を合計し、その結果を左のオペランドに割り当てます。
-=左のオペランド値から右のオペランド値を減算し、その結果を左のオペランドに割り当てます。
* =左と右のオペランド値を乗算し、その結果を左のオペランドに割り当てます。
/ =左のオペランド値を右のオペランド値で除算し、その結果を左のオペランドに割り当てます。
%=左オペランドのモジュラスを右オペランドで除算し、結果のモジュラスを左オペランドに割り当てます。

例–代入演算子


<html>
 
   <head>
    <title> JavaScript Assignment Operator Examples</title>
   </head>
 
   <body>
 
      <script type = "text/javascript">
 
            var x = 50;
            var y = 100;
            var breakLine = "<br />";
       
         
            document.write("Value of a ===output===> (x = y) ===output===> ");
            output = (x = y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x += y) ===output===> ");
            output = (x += y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x -= y) ===output===> ");
            output = (x -= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x *= y) ===output===> ");
            output = (x *= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x /= y) ===output===> ");
            output = (x /= y);
            document.write(output);
            document.write(breakLine);
          
            document.write("Value of a ===output===> (x %= y) ===output===> ");
            output = (x %= y);
            document.write(output);
            document.write(breakLine);
   
      </script>     
 
   </body>
 
</html>

Output

Value of a ===output===> (x = y) ===output===> 100
Value of a ===output===> (x += y) ===output===> 200
Value of a ===output===> (x -= y) ===output===> 100
Value of a ===output===> (x *= y) ===output===> 10000
Value of a ===output===> (x /= y) ===output===> 100
Value of a ===output===> (x %= y) ===output===> 0

 

三項演算子

JavaScriptには、三項演算子と呼ばれる特別な演算子が含まれていますか?ある条件に基づいて変数に値を割り当てます。これは、if-else条件の短縮形のようなものです。

言い換えると、 条件付き(三項)演算子 は、3つのオペランドをとる唯一のJavaScript演算子です。この演算子は、if-elseステートメントのショートカットとして頻繁に使用されます。
 

構文:

<condition> ? <value1> : <value2>;

三項演算子は条件式で始まり、その後に続きますか?オペレーター。条件が真であることが判明した場合、2番目の部分(?の後および前:演算子)が実行されます。条件がfalseになると、3番目の部分(後🙂が実行されます)。

例–JavaScriptの三項演算子

var a = 10, b = 5;
 
var c = a > b? a : b; // value of c would be 10
var d = a > b? b : a; // value of d would be 5

結論

このチュートリアルでは、例を使用してJavaScript演算子を学習しました。

リンク:https://www.tutsmake.com/javascript-operators-by-example/

#javascript