ホームページ >ウェブフロントエンド >jsチュートリアル >JavaScript 演算子を理解する: 例を含む完全ガイド

JavaScript 演算子を理解する: 例を含む完全ガイド

Linda Hamilton
Linda Hamiltonオリジナル
2024-12-18 00:15:10250ブラウズ

Understanding JavaScript Operators: A Complete Guide with Examples

### JavaScript の演算子

JavaScript の演算子は、値や変数の演算を実行するために使用される特殊な記号です。これらの操作には、算術、代入、比較、論理などのアクションが含まれる場合があります。演算子を理解することは、基本的な計算、比較を実行し、コードのフローを制御するために不可欠です。

JavaScript はさまざまな演算子をサポートしており、それらは次のタイプに分類されます。


### 1. **算術演算子**
算術演算子は、数値の数学的計算を実行するために使用されます。

Operator Description Example
Addition 5 3 → 8
- Subtraction 5 - 3 → 2
* Multiplication 5 * 3 → 15
/ Division 5 / 3 → 1.666...
% Modulus (Remainder) 5 % 3 → 2
** Exponentiation 5 ** 2 → 25

例:

let a = 10;
let b = 2;
console.log(a + b);  // Output: 12
console.log(a - b);  // Output: 8
console.log(a * b);  // Output: 20
console.log(a / b);  // Output: 5
console.log(a % b);  // Output: 0
console.log(a ** b); // Output: 100

**

2. 代入演算子**

代入演算子は変数に値を代入するために使用されます。

Operator Description Example
= Assign value x = 5
= Add and assign x = 3 → x = x 3
-= Subtract and assign x -= 2 → x = x - 2
*= Multiply and assign x *= 4 → x = x * 4
/= Divide and assign x /= 2 → x = x / 2
%= Modulus and assign x %= 3 → x = x % 3
**= Exponentiation and assign x **= 2 → x = x ** 2

例:

let a = 10;
let b = 2;
console.log(a + b);  // Output: 12
console.log(a - b);  // Output: 8
console.log(a * b);  // Output: 20
console.log(a / b);  // Output: 5
console.log(a % b);  // Output: 0
console.log(a ** b); // Output: 100

### 3. **比較演算子**
比較演算子は、値を比較し、条件に基づいてブール値 (true または false) を返すために使用されます。

Operator Description Example
== Equal to (loose) 5 == '5' → true
=== Equal to (strict) 5 === '5' → false
!= Not equal to (loose) 5 != '5' → false
!== Not equal to (strict) 5 !== '5' → true
> Greater than 5 > 3 → true
Less than 5
>= Greater than or equal 5 >= 5 → true
Less than or equal 5

例:

let x = 10;
x += 5;  // x = x + 5 -> 15
x *= 2;  // x = x * 2 -> 30
console.log(x);  // Output: 30

### 4. **論理演算子**
論理演算子は論理演算を実行し、ブール値を返すために使用されます。

Operator Description Example
&& Logical AND true && false → false
` `
! Logical NOT !true → false

#### 例:

console.log(5 == '5');  // Output: true (loose comparison)
console.log(5 === '5'); // Output: false (strict comparison)
console.log(10 > 5);    // Output: true
console.log(3 




<hr>

<p><strong>### 5. **単項演算子</strong>**<br>
単項演算子は単一のオペランドを操作して特定のアクションを実行します。</p>

<div><table>
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>  </td>
<td>Increment</td>
<td>
x   → x = x   1
</td>
</tr>
<tr>
<td>--</td>
<td>Decrement</td>
<td>
x-- → x = x - 1
</td>
</tr>
<tr>
<td>typeof</td>
<td>Type of operand</td>
<td>
typeof x → number
</td>
</tr>
<tr>
<td>void</td>
<td>Evaluates expression without returning a value</td>
<td>void(0)</td>
</tr>
</tbody>
</table></div>

<p><strong>#### 例:</strong><br>
</p>

<pre class="brush:php;toolbar:false">let a = true;
let b = false;
console.log(a && b);  // Output: false
console.log(a || b);  // Output: true
console.log(!a);      // Output: false

### 6. **三項 (条件) 演算子
**三項演算子は、if...else ステートメントの短縮形です。条件を評価し、条件が true か false に基づいて 2 つの値のいずれかを返します。

Operator Description Example
condition ? expr1 : expr2 If condition is true, return expr1; otherwise, return expr2 x > 10 ? 'Greater' : 'Lesser'

*#### 例:
*

let a = 10;
let b = 2;
console.log(a + b);  // Output: 12
console.log(a - b);  // Output: 8
console.log(a * b);  // Output: 20
console.log(a / b);  // Output: 5
console.log(a % b);  // Output: 0
console.log(a ** b); // Output: 100

### 7. **ビット演算子
**ビット演算子は 2 進数の演算を実行します。

Operator Description Example
& AND 5 & 3 → 1
` ` OR
^ XOR 5 ^ 3 → 6
~ NOT ~5 → -6
Left shift 5
>> Right shift 5 >> 1 → 2
>>> Unsigned right shift 5 >>> 1 → 2

*#### 例:
*

let x = 10;
x += 5;  // x = x + 5 -> 15
x *= 2;  // x = x * 2 -> 30
console.log(x);  // Output: 30

### 8. **拡散演算子 (...)
**スプレッド演算子を使用すると、配列またはオブジェクトの要素を新しい配列またはオブジェクトに解凍できます。

*#### 例:
*

console.log(5 == '5');  // Output: true (loose comparison)
console.log(5 === '5'); // Output: false (strict comparison)
console.log(10 > 5);    // Output: true
console.log(3 




<hr>

<p><strong>### 結論</strong></p>

<p>JavaScript 演算子は、計算、比較、論理演算を実行するための基本です。値を操作する場合でも、値を比較する場合でも、プログラムのフローを制御する場合でも、効率的なコーディングにはこれらの演算子を理解することが重要です。タスクに基づいて適切な演算子を使用して、クリーンで読みやすいコードを確保します。</p>

<p>こんにちは、アバイ・シン・カタヤットです!<br>
私はフロントエンドとバックエンドの両方のテクノロジーの専門知識を持つフルスタック開発者です。私はさまざまなプログラミング言語やフレームワークを使用して、効率的でスケーラブルでユーザーフレンドリーなアプリケーションを構築しています。<br>
ビジネス用メールアドレス kaashshorts28@gmail.com までお気軽にご連絡ください。</p>


          

            
        

以上がJavaScript 演算子を理解する: 例を含む完全ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。