JavaScript演算子リファレンス
JavaScript演算子は、値の割り当て、値の比較、算術演算の実行などに使用されます。
JavaScript算術演算子
算術演算子は、変数や値の間で算術演算を実行するために使用されます。
y = 5の場合、次の表で算術演算子を説明します。
Operator | Description | Example | Result in y | Result in x | Try it |
---|---|---|---|---|---|
+ | Addition | x = y + 2 | y = 5 | x = 7 | |
- | Subtraction | x = y - 2 | y = 5 | x = 3 | |
* | Multiplication | x = y * 2 | y = 5 | x = 10 | |
/ | Division | x = y / 2 | y = 5 | x = 2.5 | |
% | Modulus (division remainder) | x = y % 2 | y = 5 | x = 1 | |
++ | Increment | x = ++y | y = 6 | x = 6 | |
x = y++ | y = 6 | x = 5 | |||
-- | Decrement | x = --y | y = 4 | x = 4 | |
x = y-- | y = 4 | x = 5 |
算術演算子に関するチュートリアルについては、 JavaScript算術チュートリアルをお読みください。
JavaScript代入演算子
代入演算子は、JavaScript変数に値を代入するために使用されます。
x = 10およびy = 5の場合、次の表で代入演算子を説明します。
Operator | Example | Same As | Result in x | Try it |
---|---|---|---|---|
= | x = y | x = y | x = 5 | |
+= | x += y | x = x + y | x = 15 | |
-= | x -= y | x = x - y | x = 5 | |
*= | x *= y | x = x * y | x = 50 | |
/= | x /= y | x = x / y | x = 2 | |
%= | x %= y | x = x % y | x = 0 |
代入演算子に関するチュートリアルについては、 JavaScript代入チュートリアルをお読みください。
JavaScript文字列演算子
+演算子、および+ =演算子を使用して、文字列を連結(追加)することもできます。
text1 = "Good"、text2 = "Morning"、 およびtext3 = ""とすると、次の表で演算子を説明します。
Operator | Example | text1 | text2 | text3 | Try it |
---|---|---|---|---|---|
+ | text3 = text1 + text2 | "Good " | "Morning" | "Good Morning" | |
+= | text1 += text2 | "Good Morning" | "Morning" | "" |
比較演算子
比較演算子は、変数または値間の同等性または差異を判別するために論理ステートメントで使用されます。
x = 5の場合、次の表で比較演算子を説明します。
Operator | Description | Comparing | Returns | Try it |
---|---|---|---|---|
== | equal to | x == 8 | false | |
x == 5 | true | |||
=== | equal value and equal type | x === "5" | false | |
x === 5 | true | |||
!= | not equal | x != 8 | true | |
!== | not equal value or not equal type | x !== "5" | true | |
x !== 5 | false | |||
> | greater than | x > 8 | false | |
< | less than | x < 8 | true | |
>= | greater than or equal to | x >= 8 | false | |
<= | less than or equal to | x <= 8 | true |
比較演算子に関するチュートリアルについては、 JavaScript比較チュートリアルをお読みください。
条件付き(三項)演算子
条件演算子は、条件に基づいて変数に値を割り当てます。
Syntax | Example | Try it |
---|---|---|
variablename = (condition) ? value1:value2 | voteable = (age < 18) ? "Too young":"Old enough"; |
説明された例:変数「age」が18未満の値である場合、変数「voteable」の値は「Too young」になり、そうでない場合、voteableの値は「Old十分」になります。
論理演算子
論理演算子は、変数または値の間の論理を決定するために使用されます。
x = 6およびy = 3の場合、次の表で論理演算子を説明します。
Operator | Description | Example | Try it |
---|---|---|---|
&& | and | (x < 10 && y > 1) is true | |
|| | or | (x === 5 || y === 5) is false | |
! | not | !(x === y) is true |
JavaScriptビット演算子
ビット演算子は32ビットの数値で機能します。演算の数値オペランドは、32ビットの数値に変換されます。結果はJavaScript番号に変換されます。
Operator | Description | Example | Same as | Result | Decimal |
---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | x = ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
上記の例では、4ビットの符号なしの例を使用しています。ただし、JavaScriptは32ビットの符号付き数値を使用します。
このため、JavaScriptでは、〜5は10を返しません。-6を返します。
〜00000000000000000000000000000101は11111111111111111111111111111010を返します
演算子のタイプ
typeof演算子は、変数、オブジェクト、関数、または式の型を返します。
例
typeof "John"
// Returns string
typeof 3.14
// Returns number
typeof NaN
// Returns number
typeof false
// Returns boolean
typeof [1, 2, 3, 4] // Returns object
typeof {name:'John', age:34}
// Returns object
typeof new Date()
// Returns object
typeof function () {} // Returns function
typeof myCar
// Returns undefined (if myCar is not declared)
typeof null
// Returns object
以下を遵守してください:
- NaNのデータ型は数値です
- 配列のデータ型はオブジェクトです
- 日付のデータ型はオブジェクトです
- nullのデータ型はオブジェクトです
- 未定義の変数のデータ型は未定義です
typeofを使用して、JavaScriptオブジェクトが配列(または日付)であるかどうかを定義することはできません。
削除演算子
削除演算子は、オブジェクトからプロパティを削除します。
例
const person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
delete person.age; // or delete person["age"];
削除演算子は、プロパティの値とプロパティ自体の両方を削除します。
削除後、プロパティは再び追加されるまで使用できません。
削除演算子は、オブジェクトのプロパティで使用するように設計されています。変数や関数には影響しません。
注:削除演算子は、事前定義されたJavaScriptオブジェクトのプロパティでは使用しないでください。アプリケーションがクラッシュする可能性があります。
in演算子
in演算子は、指定されたプロパティが指定されたオブジェクトにある場合はtrueを返し、そうでない場合はfalseを返します。
例
// Arrays
const cars = ["Saab", "Volvo", "BMW"];
"Saab" in cars // Returns false (specify the index number instead of value)
0 in cars // Returns true
1 in cars // Returns true
4 in cars // Returns false (does not exist)
"length" in cars // Returns true (length is an Array property)
// Objects
const person = {firstName:"John", lastName:"Doe", age:50};
"firstName" in person // Returns true
"age" in person // Returns true
// Predefined objects
"PI" in Math // Returns true
"NaN" in Number // Returns true
"length" in String // Returns true
演算子のインスタンス
指定されたオブジェクトが指定されたオブジェクトのインスタンスである場合、instanceof演算子はtrueを返します。
例
const cars = ["Saab", "Volvo", "BMW"];
(cars instanceof Array) // Returns true
(cars instanceof Object) // Returns true
(cars instanceof String) // Returns false
(cars instanceof Number) // Returns false
void演算子
void演算子は式を評価し、undefinedを 返します。この演算子は、「void(0)」を使用して未定義のプリミティブ値を取得するためによく使用されます(戻り値を使用せずに式を評価する場合に便利です)。
例
<a href="#;">
Useless link
</a>
<a href="javascript:void(document.body.style.backgroundColor='red');">
Click me to change the background color of body to red
</a>