Javascript-operators
JavaScript-演算子
オペレーターとは?
単純な式 4 + 5は9 に等しいとします。 ここで、4と5は*オペランド*と呼ばれ、「+」は*演算子*と呼ばれます。 JavaScriptは、次のタイプの演算子をサポートしています。
- 算術演算子
- 比較演算子
- 論理(またはリレーショナル)演算子
- 割り当て演算子 *条件付き(または3項)演算子
すべての演算子を1つずつ見てみましょう。
算術演算子
JavaScriptは、次の算術演算子をサポートしています-
変数Aが10を保持し、変数Bが20を保持すると仮定します-
Sr.No. | Operator & Description |
---|---|
1 |
2つのオペランドを追加します 例: A + Bは30を与える |
2 |
最初のオペランドから2番目のオペランドを減算します 例: A-Bは-10を与える |
3 |
両方のオペランドを乗算します 例: *A Bは200を与える |
4 |
/(Division) 分子を分母で除算する
|
5 |
整数除算の剰余を出力します
|
6 |
整数値を1つ増やします 例: A ++は11 |
7 |
整数値を1減らします 例: A-- 9 |
注意-加算演算子()は、数値および文字列に対して機能します。 e.g. 「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.No. | Operator & Description |
---|---|
1 |
= = (Equal) 2つのオペランドの値が等しいかどうかを確認し、等しい場合は条件が真になります。 例:(A == B)は正しくありません。 |
2 |
!= (Not Equal) 2つのオペランドの値が等しいかどうかをチェックし、値が等しくない場合は条件が真になります。 例:(A!= B)は真です。 |
3 |
> (Greater than) 左のオペランドの値が右のオペランドの値よりも大きいかどうかをチェックし、そうであれば条件は真になります。 例:(A> B)は正しくありません。 |
4 |
< (Less than) 左のオペランドの値が右のオペランドの値よりも小さいかどうかを確認し、そうであれば条件は真になります。 例:(A <B)は真です。 |
5 |
>= (Greater than or Equal to) 左のオペランドの値が右のオペランドの値以上かどうかをチェックし、そうであれば条件は真になります。 例:(A> = B)は正しくありません。 |
6 |
⇐ (Less than or Equal to) 左のオペランドの値が右のオペランドの値以下かどうかをチェックし、そうであれば条件は真になります。 例:(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.No. | Operator & Description |
---|---|
1 |
&& (Logical AND) 両方のオペランドがゼロ以外の場合、条件は真になります。 例:(A && B)はtrueです。 |
2 | * |
(Logical OR)* 2つのオペランドのいずれかがゼロ以外の場合、条件は真になります。 例:(A | |
B)はtrueです。 | |
3 |
! (Logical NOT) オペランドの論理状態を反転します。 条件が真の場合、Logical NOT演算子はそれを偽にします。 例:! (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.No. | Operator & Description |
---|---|
1 |
& (Bitwise AND) 整数引数の各ビットに対してブールAND演算を実行します。 例:(A&B)は2です。 |
2 | * |
(BitWise OR)* 整数引数の各ビットに対してブールOR演算を実行します。 例:(A |
B)は3です。 |
3 |
^ (Bitwise XOR) 整数引数の各ビットに対してブール排他的OR演算を実行します。 排他的ORは、オペランド1が真であるか、オペランド2が真であり、両方ではないことを意味します。 例:(A ^ B)は1です。 |
4 |
~ (Bitwise Not) これは単項演算子であり、オペランドのすべてのビットを反転することにより動作します。 例:(〜B)は-4です。 |
5 |
<< (Left Shift) 第1オペランドのすべてのビットを、第2オペランドで指定された桁数だけ左に移動します。 新しいビットはゼロで埋められます。 値を1ポジション左にシフトすることは、2を乗算することと同等です。2ポジションをシフトすることは、4を乗算することと同等です。 例:(A << 1)は4です。 |
6 |
>> (Right Shift) バイナリ右シフト演算子。 左のオペランドの値は、右のオペランドで指定されたビット数だけ右に移動します。 例:(A >> 1)は1です。 |
7 |
>>> (Right shift with Zero) この演算子は、>>演算子と同じですが、左にシフトインされるビットが常にゼロである点が異なります。 例:(A >>> 1)は1です。 |
例
JavaScriptでビット演算子を実装するには、次のコードを試してください。
<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.No. | Operator & Description |
---|---|
1 |
= (Simple Assignment ) 右側のオペランドから左側のオペランドに値を割り当てます
|
2 |
右オペランドを左オペランドに追加し、結果を左オペランドに割り当てます。
|
3 |
左のオペランドから右のオペランドを減算し、結果を左のオペランドに割り当てます。
|
4 |
右オペランドと左オペランドを乗算し、結果を左オペランドに割り当てます。 例: *C = AはC = C *Aと同等 |
5 |
/= (Divide and Assignment) 左のオペランドを右のオペランドで除算し、結果を左のオペランドに割り当てます。
|
6 |
2つのオペランドを使用してモジュラスを取得し、結果を左のオペランドに割り当てます。
|
注-ビット単位演算子にも同じロジックが適用されるため、⇐=、>> =、>> =、&=、| =、^ =のようになります。
例
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で非常に役立つ2つの演算子について説明します。条件演算子(? :)および* typeof演算子*。
条件演算子(? :)
条件演算子は、最初に式の真または偽の値を評価してから、評価の結果に応じて2つの指定されたステートメントのいずれかを実行します。
Sr.No. | Operator and Description |
---|---|
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演算子
*typeof* 演算子は、単一のオペランドの前に置かれる単項演算子であり、任意の型にすることができます。 その値は、オペランドのデータ型を示す文字列です。
_typeof_演算子は、オペランドが数値、文字列、またはブール値の場合、「数値」、「文字列」、または「ブール値」に評価され、評価に基づいてtrueまたはfalseを返します。
*typeof* 演算子の戻り値のリストを次に示します。
Type | String Returned by typeof |
---|---|
Number | "number" |
String | "string" |
Boolean | "boolean" |
Object | "object" |
Function | "function" |
Undefined | "undefined" |
Null | "object" |
例
次のコードは、 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...