Typescript-operators
TypeScript-演算子
オペレーターとは?
演算子は、データに対して実行される機能を定義します。 演算子が機能するデータはオペランドと呼ばれます。 次の式を考慮してください-
*7+ 5 = 12*
ここでは、値7、5、および12は*オペランド*ですが、+および=は*演算子*です。
TypeScriptの主要な演算子は次のように分類できます-
- 算術演算子
- 論理演算子
- 関係演算子
- ビットごとの演算子
- 割り当て演算子
- 三項/条件演算子
- 文字列演算子
- タイプ演算子
算術演算子
変数aおよびbの値がそれぞれ10および5であると仮定します。
リンク:/typescript/typescript_arithmetic_operators_examples [例を表示]
Operator | Description | Example |
---|---|---|
PLUS (Addition) | returns the sum of the operands | a PLUS b is 15 |
- (Subtraction) | returns the difference of the values | a - b is 5 |
AST (Multiplication) | returns the product of the values | a AST b is 50 |
/(Division) | performs division operation and returns the quotient | a/b is 2 |
% (Modulus) | performs division operation and returns the remainder | a % b is 0 |
PLUSPLUS (Increment) | Increments the value of the variable by one | aPLUSPLUS is 11 |
— (Decrement) | Decrements the value of the variable by one | a-- is 9 |
関係演算子
関係演算子は、2つのエンティティ間の関係の種類をテストまたは定義します。 関係演算子はブール値、つまりtrue/falseを返します。
Aの値が10で、Bが20であると仮定します。
link:/typescript/typescript_relational_operators_examples [例を表示]
Operator | Description | Example |
---|---|---|
> | Greater than | (A > B) is False |
< | Lesser than | (A < B) is True |
>= | Greater than or equal to | (A >= B) is False |
⇐ | Lesser than or equal to | (A ⇐ B) is True |
== | Equality | (A == B) is false |
!= | Not equal | (A != B) is True |
論理演算子
論理演算子は、2つ以上の条件を結合するために使用されます。 論理演算子もブール値を返します。 変数Aの値が10で、Bが20であると仮定します。
link:/typescript/typescript_logical_operators_examples [例を表示]
Operator | Description | Example |
---|---|---|
&& (And) | The operator returns true only if all the expressions specified return true | (A > 10 && B > 10) is False |
(OR) | ||
The operator returns true if at least one of the expressions specified return true | (A > 10 | |
B >10) is True | ! (NOT) | The operator returns the inverse of the expression’s result. For E.g.: !(>5) returns false |
ビット演算子
変数A = 2およびB = 3を想定
リンク:/typescript/typescript_bitwise_operators_examples [例を表示]
Operator | Description | Example |
---|---|---|
& (Bitwise AND) | It performs a Boolean AND operation on each bit of its integer arguments. | (A & B) is 2 |
(BitWise OR) | It performs a Boolean OR operation on each bit of its integer arguments. | |
(A | B) is 3 | ^ (Bitwise XOR) |
It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. | (A ^ B) is 1 | ~ (Bitwise Not) |
It is a unary operator and operates by reversing all the bits in the operand. | (~B) is -4 | << (Left Shift) |
It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on. | (A << 1) is 4 | >> (Right Shift) |
Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. | (A >> 1) is 1 | >>> (Right shift with Zero) |
割り当て演算子
link:/typescript/typescript_assignment_operators_examples [例を表示]
Operator | Description | Example |
---|---|---|
= (Simple Assignment) | Assigns values from the right side operand to the left side operand | C = A PLUS B will assign the value of A PLUS B into C |
PLUS= (Add and Assignment) | It adds the right operand to the left operand and assigns the result to the left operand. | C PLUS= A is equivalent to C = C PLUS A |
-= (Subtract and Assignment) | It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
AST= (Multiply and Assignment) | It multiplies the right operand with the left operand and assigns the result to the left operand. | C AST= A is equivalent to C = C AST A |
/= (Divide and Assignment) | It divides the left operand with the right operand and assigns the result to the left operand. |
注意-ビット単位演算子にも同じロジックが適用されるため、⇐=、>> =、>> =、&=、| =、^ =になります。
その他の演算子
否定演算子(-)
値の符号を変更します。 例を見てみましょう。
var x:number = 4
var y = -x;
console.log("value of x: ",x); //outputs 4
console.log("value of y: ",y); //outputs -4
コンパイル時に、次のJavaScriptコードが生成されます。
//Generated by typescript 1.8.10
var x = 4;
var y = -x;
console.log("value of x: ", x); //outputs 4
console.log("value of y: ", y); //outputs -4
それは次の出力を生成します-
value of x: 4
value of y: -4
文字列演算子:連結演算子(&plus;)
&plus;演算子を文字列に適用すると、2番目の文字列が最初の文字列に追加されます。 次の例は、この概念を理解するのに役立ちます。
var msg:string = "hello"+"world"
console.log(msg)
コンパイル時に、次のJavaScriptコードが生成されます。
//Generated by typescript 1.8.10
var msg = "hello" + "world";
console.log(msg);
それは次の出力を生成します-
helloworld
連結操作は、文字列間にスペースを追加しません。 1つのステートメントで複数の文字列を連結できます。
条件演算子(?)
この演算子は、条件式を表すために使用されます。 条件演算子は、三項演算子とも呼ばれます。 構文は以下のとおりです-
Test ? expr1 : expr2
- テスト-条件式を参照
- expr1 -条件が真の場合に返される値
- expr2 -条件が偽の場合に返される値
次のコードを見てみましょう-
var num:number = -2
var result = num > 0 ?"positive":"non-positive"
console.log(result)
行2は、変数 num の値がゼロより大きいかどうかをチェックします。 num がゼロより大きい値に設定されている場合、文字列「positive」を返します。それ以外の場合、文字列「non-positive」を返します。
コンパイル時に、次のJavaScriptコードが生成されます。
//Generated by typescript 1.8.10
var num = -2;
var result = num > 0 ? "positive" : "non-positive";
console.log(result);
上記のコードスニペットは、次の出力を生成します-
non-positive
タイプ演算子
typeof演算子
これは単項演算子です。 この演算子は、オペランドのデータ型を返します。 次の例を見てください-
var num = 12
console.log(typeof num); //output: number
コンパイル時に、次のJavaScriptコードが生成されます。
//Generated by typescript 1.8.10
var num = 12;
console.log(typeof num); //output: number
それは次の出力を生成します-
number
instanceof
この演算子は、オブジェクトが指定されたタイプであるかどうかをテストするために使用できます。 instanceof 演算子の使用については、 classes の章で説明します。